GetHicon memory leak

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

I'm using GetHicon to create a Cursor from a bitmap but I'm getting a memory
leak when I do so. If I use DestroyIcon then memory leak goes away but the
newly created Cursor is invalid. Is this a known problem or am I doing
something wrong? I don't want to leak handles but I have to have my Cursor,
too. Suggestions?

After removing extra code involved in handling the hotspot issue and other
extraneous drawing things, this is the problematic code:

public Cursor MakeCursor(Bitmap bm)
{
IntPtr ptr = bm.GetHicon();
Cursor retCursor = new Cursor(ptr);

// If this line is included then returned Cursor is invalid
// If not included then Task Manager indicates handle leaks.
DestroyIcon(ptr);

return retCursor;
}
 
D

Dustin Campbell

I'm using GetHicon to create a Cursor from a bitmap but I'm getting a
memory leak when I do so. If I use DestroyIcon then memory leak goes
away but the newly created Cursor is invalid. Is this a known problem
or am I doing something wrong? I don't want to leak handles but I have
to have my Cursor, too. Suggestions?

After removing extra code involved in handling the hotspot issue and
other extraneous drawing things, this is the problematic code:

public Cursor MakeCursor(Bitmap bm)
{
IntPtr ptr = bm.GetHicon();
Cursor retCursor = new Cursor(ptr);
// If this line is included then returned Cursor is invalid
// If not included then Task Manager indicates handle leaks.
DestroyIcon(ptr);
return retCursor;
}

When you create a cursor with a handle, the cursor doesn't own that handle.
So, the handle will not be destroyed when you call the cursor's Dispose method.
You should destroy the handle manually before you dispose the cursor like
this:

DestroyIcon(myCursor.Handle);
myCursor.Dispose();

Best Regards,
Dustin Campbell
Developer Express Inc
 
R

Richard Lewis Haggard

I was rather afraid such would be the case. Since Cursor is a sealed class,
it isn't possible to derive a CursorEx class which would properly clean up
after itself. It sounds like creating a cursor on the fly using GetHicon is
not a viable approach for anyone despite the numerous examples using this
technique.
 
D

Dustin Campbell

I was rather afraid such would be the case. Since Cursor is a sealed
class, it isn't possible to derive a CursorEx class which would
properly clean up after itself.

That's true but it's certainly possible to use composition instead of inheritance.
For example:

public class BitmapCursor: IDisposable
{
private Cursor _InnerCursor;

public BitmapCursor(Bitmap bm)
{
IntPtr ptr = bm.GetHicon();
_InnerCursor = new Cursor(ptr);
}

public void Dispose()
{
DestoryIcon(_InnerCursor.Handle);
_InnerCursor.Dispose();
}

public Cursor InnerCursor { get { return _InnerCursor; } }
}

NOTE: This code is just for demonstration. A fully-fleshed out class would
include a proper IDisposable implementation and perhaps an implicit conversion
to System.Windows.Forms.Cursor.

Best Regards,
Dustin Campbell
Developer Express Inc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top