Dispose Unmanaged resources

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: Memory Leak in VB6 with strings and UDTs
    ... I have found what I think is a memory leak at the core of Visual Basic 6, ... When, in a loop over my arrays, I set BString1 to an element of an array, ... I have tried casting the number to a string with CStr but that doesn't help ... Dim bytearrayAs Byte ...
    (microsoft.public.vb.general.discussion)
  • RE: Can this cause memory leak ?
    ... No memory leak to worry about. ... it is inefficient and bad practice to catch exceptions ... during the normal flow of your code. ... > But I worry there is memory leak for lack the Deletecall. ...
    (microsoft.public.vc.language)
  • Re: another memory leak, weird`
    ... when I input a few charactors, like 10 charactors, no memory leak ... The string 'a' has not yet gone out of scope and has not been destroyed ... the heap is reported as a leak. ...
    (microsoft.public.vc.language)
  • Re: Is how to lay out the constant string compiler dependent?
    ... memory of the string that you have used except store the address in a ... the memory as the general memory leak. ... a literal array that is modifiable cannot ...
    (comp.lang.c)
  • Re: ctypes: delay conversion from c_char_p to string
    ... way to do this with ctypes? ... # without giving me an opportunity to destroy the original string. ... this technique for getting pointer *is* mentioned in ctypes documentation. ... I believe this will cause a memory leak, ...
    (comp.lang.python)