I have a ref sealed abstract class (.NET) that all its members are static and it has a static constructor. I wnat to do something at the end of my work with the class, can I have a destructor in some way?
Printable View
I have a ref sealed abstract class (.NET) that all its members are static and it has a static constructor. I wnat to do something at the end of my work with the class, can I have a destructor in some way?
No, sealed abstract classes cannot have destructors. Since it's both sealed and abstract (or, in C# terms, static), you cannot create an instance of the class. Because you don't have an instance, you'd never run the destructor either. The class will finally go away once the application domain is unloaded. At that point, what could you really want to do?
On a semi-related note, static events can cause big problems for the garbage collector if you don't unsubscribe from them. Since static events don't go away until the application domain is unloaded and events contain a reference to every object that is subcribed to them, if you don't unsubscribe from them before releasing all references to an object, then at best you'll just have a chunk of unusable memory. At worst, you'll have an object still responding to events that you didn't expect.
So I added a method that should be called when we finished with that calss.Quote:
No, sealed abstract classes cannot have destructors.
This isn't the rigth forum to ask about .Net implementations of C++.
Quote:
On a semi-related note, static events can cause big problems for the garbage collector if you don't unsubscribe from them. Since static events don't go away until the application domain is unloaded and events contain a reference to every object that is subcribed to them, if you don't unsubscribe from them before releasing all references to an object, then at best you'll just have a chunk of unusable memory. At worst, you'll have an object still responding to events that you didn't expect.
So if we have
Object ^ obj = gcnew Object;
Should be enough for GC right?Code:obj = nullptr;
That's enough for that reference. Objects are only collected if all references to them are released. Since events keep a reference to each object that's subscribed to them, just setting all of your explicit references to nullptr isn't enough; you also have to unsubscribe from every event whose lifetime is longer than that of your object. Since static events are around until the application domain is unloaded, that basically means that your objects will be around until the application closes unless you unsubscribe from them.Quote:
Originally Posted by siavoshkc
OK, I'll keep that in mind, thanks.