Garbage collection allows a small emulation of Class_Terminate event via the finalize method. However the finalize method does not
supersede the authority of GC/CLR and it may not be instantly implemented if the GC/CLR assumes that the resource / object is still needed or in use.
It could very well be a couple of calls too late before it’s shut down.
This can kill you, especially when you need to remove the object for program flow.
.
For the above reasons, it is better to avoid using Finalize by itself. If you decide
to use it, make sure that all actions are avoided that could interfere with the finalize code, such as creating an instance of the finalized object after you run the finalize method, thread synchronization operations, and any exceptions from finalize method.
Resurrection is a side effect of finalization.
Sometimes we’ll be presented with a situation in which an object has been finalized but there is still a pointer to it, meaning that Garbage Collection assumes it’s alive when it’s
already been finalized.
A typical scenario is to finalize an object in order to create a new instance of the same object; if the first object is still there in finalization, the pointer points to the old object, and the object, while in finalized stage, never gets cleaned out properly because it’s got a reference from the application. It’s important
to know that if you finalize something, you set a flag or a check routine to make sure that it’s gone before you try to do anything else concerning that object type.
Happy Learning
Avoid the Usage of RAW Finalize
0 comments
Hide comments