[C#] How can I close an handle ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll but it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory leaks...

How can I do ?

Thanks

Best Regards
 
The DeleteObject should work for you to delete bitmap (HBITMAP). How did
you know that you have "small memory leaks"?
 
Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
....
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
.....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
....
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged Window
Message ? I know also compact framework service pack 3 which correct some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred
 
Hi Sergey,

I used Majerus's tools (www.phm.lu) to analyse Memory. When my application
is running I can see memory is more and more used.

Thanks
 
I believe, the problem indeed that you do not free myHdc handle?

--
Sergey Bogdanov
http://www.sergeybogdanov.com

Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged Window
Message ? I know also compact framework service pack 3 which correct some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



:

Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:
 
Are you ever disposing myBmp? Are you ever actually running out of memory?

-Chris



Freddyboy said:
Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



Alex Yakhnin said:
Which handle are you trying to de-allocate?

Could you show your code?
 
I perform a dispose on myBmp and I dispose also myHdc.
Complete Code:
// Constructor
public NDImager()
{
// Get form handle
myHandle = GetActiveWindow();
if (myHandle != IntPtr.Zero)
{
// get handle device context
myHdc = OpenNETCF.Win32.Core.GetDC(myHandle);
// build a NDImagerMessageWindow object
myMsgWnd = new NDImagerMessageWindow(this);
}
}

// An other Method which used myBmp, myHdc ...
// this code is in a switch
// I think it's here there is a problem with memory

// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);

// delete last bitmap
if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// if handle is not null
if (hBitmap != IntPtr.Zero)
{
// create bitmap with an handle
myBmp = NDBitmapBuffer.FromHbitmap(hBitmap);
// display bitmap
myPictureBox.Image = myBmp;
myPictureBox.Invalidate();

// delete handle (unmanaged ressource)
// the value which is returned by closehandle is false that's why I use
DeleteObject
DeleteObject(hBitmap);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Stop Window Message
StopRunningWithMessageWnd();
Thread.Sleep(500);

// Remember to release the device context
OpenNETCF.Win32.Core.ReleaseDC(myHandle, myHdc);
myHdc = IntPtr.Zero;

if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// Dispose Message Manager
myMsgWnd.Dispose();
myMsgWnd = null;
}
}

Do you have an idea with all code ?

Thanks

Best Regards


Are you ever disposing myBmp? Are you ever actually running out of memory?

-Chris



Freddyboy said:
Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



Alex Yakhnin said:
Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll but
it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory leaks...

How can I do ?

Thanks

Best Regards
 
Do you actually run out of memory, or are you simply seeing usage increase?

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Freddyboy said:
I perform a dispose on myBmp and I dispose also myHdc.
Complete Code:
// Constructor
public NDImager()
{
// Get form handle
myHandle = GetActiveWindow();
if (myHandle != IntPtr.Zero)
{
// get handle device context
myHdc = OpenNETCF.Win32.Core.GetDC(myHandle);
// build a NDImagerMessageWindow object
myMsgWnd = new NDImagerMessageWindow(this);
}
}

// An other Method which used myBmp, myHdc ...
// this code is in a switch
// I think it's here there is a problem with memory

// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);

// delete last bitmap
if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// if handle is not null
if (hBitmap != IntPtr.Zero)
{
// create bitmap with an handle
myBmp = NDBitmapBuffer.FromHbitmap(hBitmap);
// display bitmap
myPictureBox.Image = myBmp;
myPictureBox.Invalidate();

// delete handle (unmanaged ressource)
// the value which is returned by closehandle is false that's why I use
DeleteObject
DeleteObject(hBitmap);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Stop Window Message
StopRunningWithMessageWnd();
Thread.Sleep(500);

// Remember to release the device context
OpenNETCF.Win32.Core.ReleaseDC(myHandle, myHdc);
myHdc = IntPtr.Zero;

if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// Dispose Message Manager
myMsgWnd.Dispose();
myMsgWnd = null;
}
}

Do you have an idea with all code ?

Thanks

Best Regards


Are you ever disposing myBmp? Are you ever actually running out of
memory?

-Chris



Freddyboy said:
Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see
the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There
are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct
some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



:

Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll
but
it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory
leaks...

How can I do ?

Thanks

Best Regards
 
Hi Chris,

I just see a usage increase.

Thanks.

Chris Tacke said:
Do you actually run out of memory, or are you simply seeing usage increase?

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Freddyboy said:
I perform a dispose on myBmp and I dispose also myHdc.
Complete Code:
// Constructor
public NDImager()
{
// Get form handle
myHandle = GetActiveWindow();
if (myHandle != IntPtr.Zero)
{
// get handle device context
myHdc = OpenNETCF.Win32.Core.GetDC(myHandle);
// build a NDImagerMessageWindow object
myMsgWnd = new NDImagerMessageWindow(this);
}
}

// An other Method which used myBmp, myHdc ...
// this code is in a switch
// I think it's here there is a problem with memory

// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);

// delete last bitmap
if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// if handle is not null
if (hBitmap != IntPtr.Zero)
{
// create bitmap with an handle
myBmp = NDBitmapBuffer.FromHbitmap(hBitmap);
// display bitmap
myPictureBox.Image = myBmp;
myPictureBox.Invalidate();

// delete handle (unmanaged ressource)
// the value which is returned by closehandle is false that's why I use
DeleteObject
DeleteObject(hBitmap);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Stop Window Message
StopRunningWithMessageWnd();
Thread.Sleep(500);

// Remember to release the device context
OpenNETCF.Win32.Core.ReleaseDC(myHandle, myHdc);
myHdc = IntPtr.Zero;

if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// Dispose Message Manager
myMsgWnd.Dispose();
myMsgWnd = null;
}
}

Do you have an idea with all code ?

Thanks

Best Regards


Are you ever disposing myBmp? Are you ever actually running out of
memory?

-Chris



Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can see
the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll. There
are
some methods which send some messages and with methods I can perform a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct
some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



:

Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll
but
it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory
leaks...

How can I do ?

Thanks

Best Regards
 
Then don't worry about it. The GC doesn't free memory exactly when you
dispose an object. If you're not seeing an OOM error, then it's not a
problem.

-Chris


Freddyboy said:
Hi Chris,

I just see a usage increase.

Thanks.

Chris Tacke said:
Do you actually run out of memory, or are you simply seeing usage
increase?

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Freddyboy said:
I perform a dispose on myBmp and I dispose also myHdc.
Complete Code:
// Constructor
public NDImager()
{
// Get form handle
myHandle = GetActiveWindow();
if (myHandle != IntPtr.Zero)
{
// get handle device context
myHdc = OpenNETCF.Win32.Core.GetDC(myHandle);
// build a NDImagerMessageWindow object
myMsgWnd = new NDImagerMessageWindow(this);
}
}

// An other Method which used myBmp, myHdc ...
// this code is in a switch
// I think it's here there is a problem with memory

// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);

// delete last bitmap
if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// if handle is not null
if (hBitmap != IntPtr.Zero)
{
// create bitmap with an handle
myBmp = NDBitmapBuffer.FromHbitmap(hBitmap);
// display bitmap
myPictureBox.Image = myBmp;
myPictureBox.Invalidate();

// delete handle (unmanaged ressource)
// the value which is returned by closehandle is false that's why I use
DeleteObject
DeleteObject(hBitmap);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Stop Window Message
StopRunningWithMessageWnd();
Thread.Sleep(500);

// Remember to release the device context
OpenNETCF.Win32.Core.ReleaseDC(myHandle, myHdc);
myHdc = IntPtr.Zero;

if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// Dispose Message Manager
myMsgWnd.Dispose();
myMsgWnd = null;
}
}

Do you have an idea with all code ?

Thanks

Best Regards


:

Are you ever disposing myBmp? Are you ever actually running out of
memory?

-Chris



Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can
see
the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll.
There
are
some methods which send some messages and with methods I can perform
a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct
some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



:

Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll
but
it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory
leaks...

How can I do ?

Thanks

Best Regards
 
Hi Chris,

ok I understood. Thank you for your answer.

Best regards.

Frederic

Then don't worry about it. The GC doesn't free memory exactly when you
dispose an object. If you're not seeing an OOM error, then it's not a
problem.

-Chris


Freddyboy said:
Hi Chris,

I just see a usage increase.

Thanks.

Chris Tacke said:
Do you actually run out of memory, or are you simply seeing usage
increase?

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


I perform a dispose on myBmp and I dispose also myHdc.
Complete Code:
// Constructor
public NDImager()
{
// Get form handle
myHandle = GetActiveWindow();
if (myHandle != IntPtr.Zero)
{
// get handle device context
myHdc = OpenNETCF.Win32.Core.GetDC(myHandle);
// build a NDImagerMessageWindow object
myMsgWnd = new NDImagerMessageWindow(this);
}
}

// An other Method which used myBmp, myHdc ...
// this code is in a switch
// I think it's here there is a problem with memory

// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);

// delete last bitmap
if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// if handle is not null
if (hBitmap != IntPtr.Zero)
{
// create bitmap with an handle
myBmp = NDBitmapBuffer.FromHbitmap(hBitmap);
// display bitmap
myPictureBox.Image = myBmp;
myPictureBox.Invalidate();

// delete handle (unmanaged ressource)
// the value which is returned by closehandle is false that's why I use
DeleteObject
DeleteObject(hBitmap);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Stop Window Message
StopRunningWithMessageWnd();
Thread.Sleep(500);

// Remember to release the device context
OpenNETCF.Win32.Core.ReleaseDC(myHandle, myHdc);
myHdc = IntPtr.Zero;

if (myBmp != null)
{
myBmp.Dispose();
myBmp = null;
}

// Dispose Message Manager
myMsgWnd.Dispose();
myMsgWnd = null;
}
}

Do you have an idea with all code ?

Thanks

Best Regards


:

Are you ever disposing myBmp? Are you ever actually running out of
memory?

-Chris



Hi Alex, Hi Sergey

yes I can show my code of course.

[DllImport("coredll.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
...
// get an handle to a bitmap
IntPtr hBitmap;
hBitmap = NDCaptureUtility.CreateGreyImageInDeviceContext(myHdc);
....
// create bitmap with an handle
myBmp = BitmapBuffer.FromHbitmap(hBitmap);
...
// delete handle (unmanaged ressource) with close handle
CloseHandle(hBitmap); // the value which is return is false

// That's why I used DeleteObject
DeleteObject(hBitmap);

But when I analyse Memory with Majerus's tools (www.phm.lu), I can
see
the
memory is more and more used.

I think my problem is hBitmap.

CreateGreyImageInDeviceContext is a method which is in a C Dll.
There
are
some methods which send some messages and with methods I can perform
a
preview and I can take some pictures. Maybe a problem with unmaneged
Window
Message ? I know also compact framework service pack 3 which correct
some
bugs with the marshalling between C# and C

What do you think about this ?

Thanks

Best Regards.

Fred



:

Which handle are you trying to de-allocate?

Could you show your code?
--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

Hi,

I have an handle on a bitmap (FromHBitmap by Alex Yakhnin).
I'd like to close this handle with CloseHandle Method in core.dll
but
it
seems to me that method has no effect.

I use DeleteObject but it seems to me there are small memory
leaks...

How can I do ?

Thanks

Best Regards
 
Back
Top