newbie finalization question

J

Jim Bancroft

Hi,

I have a couple of classes that implement the Dispose() method. One class
creates an instance of and calls methods on the other, so it's a sort of
parent-child relationship I suppose.

My question is, what happens if my child class calls its Dispose method at
the end of one of its try-catch-finally blocks? In step-by-step terms, I'm
wondering about this: the parent class creates a child and calls the
child's method ABC(). The child in turn calls its Dispose() method at the
end of ABC()'s finally block. I assume the child is now out of reach and
subject to garbage collection, but I could be wrong.

Now, what if the parent calls another one of the child's methods, maybe
method XYZ()? Will it have an invalid object at that point?
 
M

Marina

Calling Dispose does not make it eligible for garbage collection.
Implementing IDisposable is just a way to make sure the GC can make sure the
object has cleaned up its resources before cleaning it up.

Now, if in you program you do the cleaning up earlier then that, then great.
But it doesn't mean the object will be garbage collected any earlier.
 
P

Phil Wilson

1. If you're saying that the child object calls iits own Dispose method out
of a method call, that's rather unusual. Dispose is for clients to call to
clean up unmanaged resources early, because client callers should know when
they've finished using the object, and this is presumably related to your
second question....
2. A Dispose implementation can save object state (say a bool m_disposed)
and return an error if it's used after it's been disposed.
 
T

twgathings

sorry this is just a test post.
1. If you're saying that the child object calls iits own Dispose method out
of a method call, that's rather unusual. Dispose is for clients to call to
clean up unmanaged resources early, because client callers should know when
they've finished using the object, and this is presumably related to your
second question....
2. A Dispose implementation can save object state (say a bool m_disposed)
and return an error if it's used after it's been disposed.
--
Phil Wilson
[Microsoft MVP-Windows Installer]

Jim Bancroft said:
Hi,

I have a couple of classes that implement the Dispose() method. One class
creates an instance of and calls methods on the other, so it's a sort of
parent-child relationship I suppose.

My question is, what happens if my child class calls its Dispose method at
the end of one of its try-catch-finally blocks? In step-by-step terms,
I'm wondering about this: the parent class creates a child and calls the
child's method ABC(). The child in turn calls its Dispose() method at the
end of ABC()'s finally block. I assume the child is now out of reach and
subject to garbage collection, but I could be wrong.

Now, what if the parent calls another one of the child's methods, maybe
method XYZ()? Will it have an invalid object at that point?

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 

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

Top