[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [DotGNU]lesson in dispose
From: |
David Podhola |
Subject: |
RE: [DotGNU]lesson in dispose |
Date: |
Sat, 15 Nov 2003 20:49:41 +0100 |
At least in Microsoft .NET the documentation
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIDisposableClassDisposeTopic.asp)
says:
When implementing this method, objects must seek to ensure that all held
resources are freed by propagating the call through the containment hierarchy.
For example, if an object A allocates an object B, and object B allocates an
object C, then A's Dispose implementation must call Dispose on B, which must in
turn call Dispose on C. Objects must also call the Dispose method of their base
class if the base class implements IDisposable.
And
If an object's Dispose method is called more than once, the object must ignore
all calls after the first one. The object must not throw an exception if its
Dispose method is called multiple times. Dispose can throw an exception if an
error occurs because a resource has already been freed and Dispose had not been
called previously.
IMHO it should be implemented the way you suggest - the question remaining is,
if it could not be done somehow magically automatically? :-)
David Podhola
-----Original Message-----
From: Neil Cawse [mailto:address@hidden
Sent: Saturday, November 15, 2003 8:33 PM
To: Rhys Weatherley
Cc: address@hidden
Subject: [DotGNU]lesson in dispose
Can you tell me if Im on the right track here with regards to disposing managed
resources. Take for example toolbar which uses imagelist, which uses
ImageList.ImageCollection, which uses Image etc..
Is it a "rule" that we should implement IDisposable if we have any local
objects who in turn implement IDisposable. And then we have to call their
Dispose method when ours is called?
Do we just leave finalizers?
Is this correct?
Toolbar:
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (imageList != null)
{
imageList.RecreateHandle -= new
EventHandler(ImageListHandler);
#if CONFIG_COMPONENT_MODEL
imageList.Dispose();
#else
imageList.Dispose(disposing);
#endif
}
}
base.Dispose(disposing);
}
In ImageList:
#if CONFIG_COMPONENT_MODEL
protected override void Dispose(bool disposing)
#else
public void Dispose(bool disposing)
#endif
{
images.Dispose(); // instance of ImageList.ImageCollection
}
In ImageList.ImageCollection:
internal void Dispose()
{
if (images == null)
return;
for (int i = 0; i < images.Count; i++)
(images[i] as Image).Dispose();
}
Thanks
Neil