Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Dispose Unmanaged resources

Reply
 
Old 03-17-2008   #1 (permalink)
Curious


 
 

Dispose Unmanaged resources

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);
}
}


My System SpecsSystem Spec
Old 03-20-2008   #2 (permalink)
Sergey Zyuzin


 
 

Re: Dispose Unmanaged resources

On Mar 17, 5:11*pm, Curious <fir5tsi...@xxxxxx> wrote:
Quote:

> 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
My System SpecsSystem Spec
Old 03-24-2008   #3 (permalink)
Duy Lam


 
 

Re: Dispose Unmanaged resources

Curious wrote:
Quote:

> 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
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to Dispose of Old Computers Responsibly Chillout Room
.net object dispose .NET General
formArray close / dispose issue .NET General
Problem getting exception info from unmanaged C++ .NET General
Implementing Unmanaged Interface in C# .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46