Where to dispose what

G

Guest

Can you please give simple examples of objects that would be disposed in both
of the below commented sections. E.g where should SqlConnection, SqlCommand,
DataSet, File, Label, collection objects go?

private override void Dispose(bool disposing)
{
if (disposing)
{
// Release managed resources (examples?)
}

// Release unmanaged resources (examples?)

base.Dispose(disposing);
}

Thanks,

Pete
 
R

Richard Blewett

Pete said:
Can you please give simple examples of objects that would be disposed in
both
of the below commented sections. E.g where should SqlConnection,
SqlCommand,
DataSet, File, Label, collection objects go?

private override void Dispose(bool disposing)
{
if (disposing)
{
myCachedConnection.Dispose();


}

// Release unmanaged resources (examples?)
InternalWin32Wrapper.CloseHandle(cachedNativeHandle);


base.Dispose(disposing);
}

Thanks,

Pete

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
G

Guest

Richard,

Thanks for your prompt response.

It seems strange (to me) that a SqlConnection object (for example) should be
disposed of in the managed section even though it implements IDisposable
because it has links to underlying data providers that are unmanaged code.
It seems like there is unmanaged code being disposed of in the managed code
section?

Also, I don't really understand the line:
InternalWin32Wrapper.CloseHandle(cachedNativeHandle);
I know it's only an example but would InternalWin32Wrapper be a COM
component or a Windows API call or something?

Many thanks,

Pete
 
R

Richard Blewett [DevelopMentor]

SqlConnection is a managed class (although it manages an unmanaged resource. The point is that SqlConnection has its own Dispose method that frees its unmanaged resource. You just have to call its Dispose method.

The InternalWin32Wrapper is a made up class that uses PInvoke to get hold of some native handle via the Win32 API (e.g. a shared memory segment). This needs to be freed up and so I have presumed that InternalWin32Wrapper also wraps the Win32 CloseHandle API to free up the resource. The handle (and so the shared memory segment) are unmanaged resources, no other managed class is going to release them apart from this one.

Lets be clear on why there are two places to put code:

The unmanaged resources that the object has acquired directly should always be cleared up (no-one else is going to do that as I've said) therefore it happens outside of the if( disposing) code block. This freeing will occur whether this method is called via IDisposable.Dispose or the object's Finalizer. However, when a group of objects end up on the finalization queue, there is no predetrmined order in which they will be enqueued. therefore, an object you refer to may have already been finalized, and if it hasn't it will be shortly. Therefore shoudl shouldn't call Dispose on managed objects from a finalizer. this is why managed resouorces are only touched during the if block - which will only be entered if the method is called from IDisposable.Dispose and not from teh objects finalizer.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Richard,

Thanks for your prompt response.

It seems strange (to me) that a SqlConnection object (for example) should be
disposed of in the managed section even though it implements IDisposable
because it has links to underlying data providers that are unmanaged code.
It seems like there is unmanaged code being disposed of in the managed code
section?

Also, I don't really understand the line:
InternalWin32Wrapper.CloseHandle(cachedNativeHandle);
I know it's only an example but would InternalWin32Wrapper be a COM
component or a Windows API call or something?

Many thanks,

Pete
 
R

Roy

Richard,
What would happen if connection pool is used? Is the connection object goes
back to the pool or destroyed when Dispose() is called?
Thanks.
 

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

Similar Threads


Top