   [.net] destructors and finalizers
  Hello, Suppose I have a managed ref class that contains native pointers.  In the constructor, I call new to allocate the objects those pointers point to.Now, I need to call delete on those pointers.  Is it true that I should not call delete in the destructor because the destructor is only called if I explicitly call delete on the managed handle?  Instead, I should be safe and put the delete in the finalizer, which will always get called eventually by the GC.  Then in the destructor, I can call the finalizer in case the client did explicitly delete the managed handle.  But if he didn't, I'm still safe because I have a finalizer.  In summary:1. GC does not call a destructor, but only calls the finalizer.2. Delete native pointers in finalizer in case the destructor is never called.Is this right?   
  The finallizer does not get called if the destructor gets called (i.e., you "delete"ed the object). The finallizer only gets called if the destructor does not get called and the object goes out of scope. Therefore, the finallizer and destructor should typically have the same logic. As you cannot explicitly call the finallizer, you must create a third method that both the finallizer and the destructor call for cleanup.   ;  Hello, In C++/CLI you can explicitly call the finalizer.  I guess I should have been more clear on the language.I think C# has finalizer and dispose, but no destructor.   ;Quote:Original post by QuatHello, In C++/CLI you can explicitly call the finalizer.  I guess I should have been more clear on the language.I think C# has finalizer and dispose, but no destructor.No C# has also has destructors, you just can't call them though only the GC can and you don't know when it's going to run so you have to be very careful.Ensure that desructors do not depend on each other or overlap with each other because if your destructor calls other objects they may already had their destructor called by the GC!;  I found this article to be very interesting, and clears up things quite a bit.http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539aeHope this helps.(I wish I had found how to insert tags properly);  If you are using C#, you should implement the IDisposable interface for all classes with native resources.The Dispose() method then becomes the destructor, and the finalizer acts only as a backup if you forget to dispose it manually.   ;Quote:Original post by daviangelQuote:Original post by QuatHello, In C++/CLI you can explicitly call the finalizer.  I guess I should have been more clear on the language.I think C# has finalizer and dispose, but no destructor.No C# has also has destructors, you just can't call them though only the GC can and you don't know when it's going to run so you have to be very careful.Ensure that desructors do not depend on each other or overlap with each other because if your destructor calls other objects they may already had their destructor called by the GC!those aren't destructors, they are finalizers.   ;Quote:Original post by RainweaverI found this article to be very interesting, and clears up things quite a bit.http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539aeHope this helps.(I wish I had found how to insert tags properly)just use HTML