By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,651 Members | 1,757 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,651 IT Pros & Developers. It's quick & easy.

Disposing Pattern

P: n/a
Hi,

Could someone explain me if I need to implement the Dispose pattern on
managed objects that hold objects which have longer life or is a
singleton?

Example -
Class A holds Class Global where Class Global is a singleton.

So should Class A have
private Global g;
#region IDisposable Members

public void Dispose() {
// TODO: Add TagDataIOExecutor.Dispose implementation
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing) {
g = null;
}

~A() {
Dispose(false);
}

#endregion
-Thanks
mb

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi mb,

Even if Global implements singleton pattern, setting "g = null" does
not mean you dispose it. It just means g will mo longer reference that
singleton's memory location. Did you omit a line like: g.Dispose() ? If
no, your class does not hold resources that need disposing, you don't
need to consider the dispose pattern at all.
Could someone explain me if I need to implement the Dispose pattern on
managed objects that hold objects which have longer life or is a
singleton?

Generally, a class should not dispose object if it does not control the
object's life time.

Regards,
Thi

Nov 17 '05 #2

P: n/a
Thank you for the reply.

Its true 'a' an instance of A does not control the life span of 'g'.
But how would GC determine that 'a' is no longer in use?

Nov 17 '05 #3

P: n/a
su***************@gmail.com wrote:
Thank you for the reply.

Its true 'a' an instance of A does not control the life span of 'g'.
But how would GC determine that 'a' is no longer in use?


The GC walks the forest of "reachable" objects, taking its "roots" from
static variables and local variables in all the stack frames in all
threads. It starts out with the assumption that everything can be
thrown away, and marks those objects which it finds can't be thrown
away.

It's more complex than that, partly due to generations, but that's the
basic thrust of it.
See http://msdn.microsoft.com/msdnmag/is...I/default.aspx for
more details.

Jon

Nov 17 '05 #4

P: n/a
Thanks you for the explanation. I tried a sample example and did
observe the same behaviour.

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.