473,382 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

GetHicon memory leak

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

--
Richard Lewis Haggard
www.Haggard-And-Associates.com
Dec 12 '06 #1
3 5455
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.
Dec 12 '06 #2
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.
--
Richard Lewis Haggard
www.Haggard-And-Associates.com

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>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.


Dec 12 '06 #3
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.
Dec 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
2
by: Smokey Grindel | last post by:
when I do this code Dim HIcon As IntPtr = bmp.GetHicon to convert a Bitmap which is just a 16x16 image I pulled from a resource originally as an icon (16x16x32bit) then converted into a bitmap to...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.