GC.ReRegisterForFinalize

  • Thread starter Thread starter Shree
  • Start date Start date
S

Shree

Hi All,
I wanted to know the use case when one needs to call
gc.reregisterforfinalize. Can anyone help? I only know that call this
function it adds its argument back to the finalization queue. When
would/should one need to do this?

Thanks.
 
Shree said:
I wanted to know the use case when one needs to call
gc.reregisterforfinalize. Can anyone help? I only know that call this
function it adds its argument back to the finalization queue. When
would/should one need to do this?

Ideally, you should never have to use this function. It is only needed if
you resurrect an object from within its Finalizer (which is not a good
idea), and you want your resurrected object to Finalize again once it
becomes again unreachable and it is garbage-collected for a second time.
Essentially, ReRegisterForFinalize adds your object (again) to the
Finalization queue, from where it had already been removed since the
Finalizer was already running.
You resurrect an object from the Finalizer if you make it Reachable (it
was unreachable or the Finalizer would not be running) by assigning a
reference to it ("this") into a reachable location such as a static
variable.
 
It is only needed if
you resurrect an object from within its Finalizer (which is not a good
idea),

Couldn't you also call SupressFinalize(this) and later do something
that once again would require the finalizer to run? (I realize that in
most cases this would mean calling Dispose() and then doing something
with the disposed object, which is a bad idea in any case)

Kevin Wienhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top