![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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); > * * * * } > * * * } I think you should use Marshal.ReleaseComObject method HTH, Sergey |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||