PC Review


Reply
Thread Tools Rate Thread

Dispose Unmanaged resources

 
 
Curious
Guest
Posts: n/a
 
      17th Mar 2008
We all know that in .NET, we don't need to worry about memory leak
becaseu GC does a nice job to collect it.

The only time we need to worry about memory leak is when we use
unmanaged resources, such as COM-based objects written in C++, for an
instance. Therefore, I've created the following sample code below to
illustrate the step necessary in disposing unmanaged resources.

I believe that to dispose unmanaged resources, we'll need to call the
Destructor of the COM object. However, I don't know what's the right
syntax. I'm looking for advice and input on the piece of code below.
Thanks!

using System;
using System.Runtime.InteropServices;

namespace iopmb
{


[DllImport("user32.dll", EntryPoint="MessageBoxW",
CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool COMDbConnection (string connectionString,
bool useTrustedConnection, string, userName, string password);

COMDbConnection myConnection = new
COMDbConnection("Provider=sqloledb;Data Source=DataWarehouse;Initial
Catalog=Pace;", true, "sa", "password") ;

try
{
// do something with myConnection
}
catch
{
finally
{
myConnection.Dispose() ;
}

}


class COMDbConnection
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Call distructor of the COM object. I know this is not right to
clean memory this way.
// But if anyone points out the right way, I'd appreciate it!
this.~COMDbConnection();
}
// This should be skipped
//base.Dispose(disposing);
}
}

 
Reply With Quote
 
 
 
 
Sergey Zyuzin
Guest
Posts: n/a
 
      20th Mar 2008
On Mar 17, 5:11*pm, Curious <fir5tsi...@yahoo.com> wrote:
> We all know that in .NET, we don't need to worry about memory leak
> becaseu GC does a nice job to collect it.
>
> The only time we need to worry about memory leak is when we use
> unmanaged resources, such as COM-based objects written in C++, for an
> instance. Therefore, I've created the following sample code below to
> illustrate the step necessary in disposing unmanaged resources.
>
> I believe that to dispose unmanaged resources, we'll need to call the
> Destructor of the COM object. However, I don't know what's the right
> syntax. I'm looking for advice and input on the piece of code below.
> Thanks!
>
> using System;
> using System.Runtime.InteropServices;
>
> * * * namespace iopmb
> * * * {
>
> * * * * * [DllImport("user32.dll", EntryPoint="MessageBoxW",
> CharSet=CharSet.Auto, ExactSpelling=true)]
> * * * * * public static extern bool COMDbConnection (string connectionString,
> bool useTrustedConnection, string, userName, string password);
>
> * * * * * COMDbConnection myConnection = new
> COMDbConnection("Provider=sqloledb;Data Source=DataWarehouse;Initial
> Catalog=Pace;", true, "sa", *"password") ;
>
> * * * * * try
> * * * * * {
> * * * * * * * * // do something with myConnection
> * * * * * }
> * * * * * catch
> * * * * * {
> * * * * * finally
> * * * * * {
> * * * * * * * * myConnection.Dispose() ;
> * * * * * }
>
> * * * }
>
> * * * class COMDbConnection
> * * * {
> * * * * protected override void Dispose(bool disposing)
> * * * * {
> * * * * * * if (disposing)
> * * * * * * {
> * * * * * * * * // Call distructor of the COM object. I know this is not right to
> clean memory this way.
> * * * * * * * * // But if anyone points out the right way,I'd appreciate it!
> * * * * * * * * this.~COMDbConnection();
> * * * * * * }
> * * * * * * // This should be skipped
> * * * * * * //base.Dispose(disposing);
> * * * * }
> * * * }


Hi,

I think you should use Marshal.ReleaseComObject method

HTH,
Sergey
 
Reply With Quote
 
Duy Lam
Guest
Posts: n/a
 
      24th Mar 2008
Curious wrote:
> We all know that in .NET, we don't need to worry about memory leak
> becaseu GC does a nice job to collect it.
>
> The only time we need to worry about memory leak is when we use
> unmanaged resources, such as COM-based objects written in C++, for an
> instance. Therefore, I've created the following sample code below to
> illustrate the step necessary in disposing unmanaged resources.
>
> I believe that to dispose unmanaged resources, we'll need to call the
> Destructor of the COM object. However, I don't know what's the right
> syntax. I'm looking for advice and input on the piece of code below.
> Thanks!
>
> using System;
> using System.Runtime.InteropServices;
>
> namespace iopmb
> {
>
>
> [DllImport("user32.dll", EntryPoint="MessageBoxW",
> CharSet=CharSet.Auto, ExactSpelling=true)]
> public static extern bool COMDbConnection (string connectionString,
> bool useTrustedConnection, string, userName, string password);
>
> COMDbConnection myConnection = new
> COMDbConnection("Provider=sqloledb;Data Source=DataWarehouse;Initial
> Catalog=Pace;", true, "sa", "password") ;
>
> try
> {
> // do something with myConnection
> }
> catch
> {
> finally
> {
> myConnection.Dispose() ;
> }
>
> }
>
>
> class COMDbConnection
> {
> protected override void Dispose(bool disposing)
> {
> if (disposing)
> {
> // Call distructor of the COM object. I know this is not right to
> clean memory this way.
> // But if anyone points out the right way, I'd appreciate it!
> this.~COMDbConnection();
> }
> // This should be skipped
> //base.Dispose(disposing);
> }
> }
>



According to Dispose pattern, you must call overload Dispose() method in
destructor but you called destructor in Dispose() method.



--
Thanks,
Duy Lam Phuong
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dispose managed resources gol Microsoft Dot NET Framework Forms 1 15th Sep 2008 03:11 AM
Static Fonts and other Unmanaged Resources - where to dispose? =?Utf-8?B?bmlq?= Microsoft Dot NET 2 17th Feb 2006 12:49 AM
*Dependent* Unmanaged Resources, Dispose and finalizers Fernando Cacciola Microsoft C# .NET 2 29th Apr 2005 07:26 PM
System resources and Dispose!? James T. Microsoft VB .NET 0 16th Mar 2005 05:23 PM
unmanaged vs: managed code dispose method Herman Microsoft ADO .NET 8 12th Jan 2004 10:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.