Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I am currently searching for GDI leaks within my MFC application.

I use Cbutton onto which I load an Icon with CButton:SetIcon() method. Then, when the owner window is destroyed, it sets back the CButton icon to NUll then calls the appropriate DestroyIcon() function.

When I look to the Windows Application Manager, I see that the number of allocated GDI objects constantly grows up, as the user opens and closes windows. And it seems to be related to the icon button : when I remove the call to SetIcon, leaving the button blank, the number of allocated GDI objects is stable !

I see that calling DestroyIcon() function when window is closing helps keeping number of GDI objects low, but it is not sufficient.

Is there a ComCtl32 v6 issue ? I mean, does the button builds more images based on the given icon to accomodate for all button states then it would 'forget' to destroy these images ?

Could anyone help me or tell me what I did wrong ?

Thanks in advance
Posted
Updated 23-Mar-18 7:07am
Comments
CPallini 7-Apr-11 13:13pm    
Probably seeing the relevant code snippets would help.

ahhh sorry !.

I forgot that my button was not a simple CButton !
I just found out that the bug is in a C++ class that I have got from CodeProject.

It is CImageButtonWithStyle :

// The class CImageButtonWithStyle is a CButton derived class that
// handles the NM_CUSTOMDRAW notification to provide XP visual styles themed
// appearance for buttons with BS_BITMAP or BS_ICON style set.
// This makes them match the appearance of normal buttons with text labels.
// The default Windows controls do not provide the themed appearance for
// these buttons.

Actually, there is some GDI leak in the code : each time a Icon-button is painted, this simple snippet of code :
HICON hIcon;
ICONINFO ii;
GetIconInfo (hIcon, &ii);


generates at most two HBITMAP that must be deleted afterwards, with:

DeleteObject (ii.hbmColor);
DeleteObject (ii.hbmMask);


Thanks, CPallini : you have forced me to look closer to the problem !
 
Share this answer
 
BTW. You don't need to use 3rd party buttons since VC 8.0. The CMFCButton (new MFC button class) has the ability to display images.
 
Share this answer
 
Comments
Richard Deeming 23-Mar-18 14:47pm    
SEVEN YEARS too late!
steveb 23-Mar-18 15:24pm    
The answer was not for him but for the others who stumbles upon this question. Especially if it is on the top of the list of other related question today.
Richard Deeming 23-Mar-18 15:25pm    
It's on top of the list because you posted a new solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900