|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Where to dispose whatof 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 "Pete" <P***@discussions.microsoft.com> wrote in message myCachedConnection.Dispose();news:7C9492ED-01E0-452A-90D0-1207EE296FC6@microsoft.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) > { > } InternalWin32Wrapper.CloseHandle(cachedNativeHandle);> > // Release unmanaged resources (examples?) > Regards> base.Dispose(disposing); > } > > Thanks, > > Pete > 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 Show quoteHide quote "Richard Blewett" wrote: > "Pete" <P***@discussions.microsoft.com> wrote in message > news:7C9492ED-01E0-452A-90D0-1207EE296FC6@microsoft.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(cachedNativeHandle); > > > > > base.Dispose(disposing); > > } > > > > Thanks, > > > > Pete > > > > Regards > > Richard Blewett - DevelopMentor > http://www.dotnetconsult.co.uk/weblog > http://www.dotnetconsult.co.uk > > > 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 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. Show quoteHide quote "Richard Blewett [DevelopMentor]" wrote: > 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 > > > |
|||||||||||||||||||||||