HighTechTalks DotNet Forums  

Where to dispose what

Dotnet Framework microsoft.public.dotnet.framework


Discuss Where to dispose what in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Pete
 
Posts: n/a

Default Where to dispose what - 11-16-2004 , 10:39 AM






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


Reply With Quote
  #2  
Old   
Richard Blewett
 
Posts: n/a

Default Re: Where to dispose what - 11-16-2004 , 11:03 AM






"Pete" <Pete (AT) discussions (DOT) microsoft.com> wrote

Quote:
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();


Quote:
}

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

Quote:
base.Dispose(disposing);
}

Thanks,

Pete

Regards

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




Reply With Quote
  #3  
Old   
Pete
 
Posts: n/a

Default Re: Where to dispose what - 11-16-2004 , 12:46 PM



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(cachedNativeHandl e);
I know it's only an example but would InternalWin32Wrapper be a COM
component or a Windows API call or something?

Many thanks,

Pete

"Richard Blewett" wrote:

Quote:
"Pete" <Pete (AT) discussions (DOT) microsoft.com> wrote in message
news:7C9492ED-01E0-452A-90D0-1207EE296FC6 (AT) microsoft (DOT) com...
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(cachedNativeHandl e);


base.Dispose(disposing);
}

Thanks,

Pete


Regards

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




Reply With Quote
  #4  
Old   
Richard Blewett [DevelopMentor]
 
Posts: n/a

Default Re: Where to dispose what - 11-16-2004 , 01:30 PM



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(cachedNativeHandl e);
I know it's only an example but would InternalWin32Wrapper be a COM
component or a Windows API call or something?

Many thanks,

Pete



Reply With Quote
  #5  
Old   
Roy
 
Posts: n/a

Default Re: Where to dispose what - 11-27-2007 , 12:16 PM



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.

"Richard Blewett [DevelopMentor]" wrote:

Quote:
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(cachedNativeHandl e);
I know it's only an example but would InternalWin32Wrapper be a COM
component or a Windows API call or something?

Many thanks,

Pete




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.