c# - Disposing my System.IDisposable object in my finalizer -


there several discussions here on stackoverflow if object manages other managed objects implement system.idisposable.

note: below not talking unmanaged code. understand importance of cleaning unmanaged code

most of discussions if object owns managed object implements system.idisposable, should implement system.idisposable, in case should call dispose() of disposable objects object holds. logical, because don't know whether disposable object own uses unmanaged code. know creator of other object thought wise if you'd call dispose don't need object anymore.

a explanation of disposable pattern given here on stackoverflow, edited community wiki:

proper use of idisposable interface

quite often, , in mentioned link read:

"you don't know order in 2 objects destroyed. entirely possible in dispose() code, managed object you're trying rid of no longer there."

this baffles me, because thought long object holds reference object x, object x not , cannot finalized.

or in other words: long object holds reference object x can object x not finalized.

if true, why be, if hold reference object until finalize, object refer finalized?

the truth somewhere between two:

  • the object can't garbage collected, possibility of object no longer "being there" isn't true
  • an object can finalized when there no longer references other non-finalizable objects.

if object x refers object y, both finalizable, it's entirely possible object y finalized before object x, or them finalized concurrently.

if assumption correct, create 2 objects refer each other (and have finalizers), , never garbage collected because never finalized.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -