Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have found a GDI leak in our huge application software.

Below is a simple program to test this problem.
The Idea is that the main dialog box opens another dialog box(dialog box A).
If the dialog box A include a bitmap function for a CStatic control,
it will create GDI leak.

Even when I use "DeleteObject(bitmap)".

Have I done some thing wrong ?
Do you have any thoughts?

Thanks.

// Resource File
  ...

  DIALOG_BOXA DIALOGEX 0, 0, 219, 142
  STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_BORDER
  EXSTYLE WS_EX_STATICEDGE
  FONT 8, "MS Shell Dlg", 400, 0, 0x1
  BEGIN
      DEFPUSHBUTTON   "OK",IDOK,46,121,50,14
      PUSHBUTTON      "Cancel",IDCANCEL,119,121,50,14
      CONTROL         131,RED_LIGHT0,"Static",SS_BITMAP,7,17,80,37
      PUSHBUTTON      "",RED_LIGHT1,7,60,80,37,BS_BITMAP | NOT WS_TABSTOP
  END

  // head file
  DialogBoxA: public CDialog
  {
     ...

     CStatic m_static;
     CButton m_button ;

     ...
  }


  /////////////////////////////////////////////////////////

  void DialogBoxA::DoDataExchange(CDataExchange* pDX)
  {
     CDialog::DoDataExchange(pDX);
     DDX_Control(pDX, RED_LIGHT0, m_static);
     DDX_Control(pDX, RED_LIGHT1, m_button);
  }

  BOOL DialogBoxA::OnInitDialog()
  {
     CDialog::OnInitDialog();

     HBITMAP bitmap ;

     // This will create GDI leak !!!
     bitmap = LoadBitmap ( AfxGetApp()->m_hInstance,BEACON_BIG_RED_ON) ;
     m_static.SetBitmap (bitmap );
     DeleteObject(bitmap);


     // This is OK !!!
     bitmap = LoadBitmap ( AfxGetApp()->m_hInstance,BEACON_BIG_RED_ON) ;
     m_button.SetBitmap (bitmap );
     DeleteObject(bitmap);

     return TRUE;  // return TRUE unless you set the focus to a control
                // EXCEPTION: OCX Property Pages should return FALSE
  }


And I use "LoadImage()" instead of "LoadBitmap()", it is same problem.
Posted

Is it, by chance, a 'false-positive'? See, for instance "Default Win32 project has constant number of memory leaks" at Stack Overflow[^].
 
Share this answer
 
Comments
wy737 21-Jan-14 14:09pm    
Thanks for your help, But that look like can not solve my problem.
Thanks every one.
I solved problem according to this website suggestion.
http://stackoverflow.com/questions/8489301/possible-memory-leak-using-gethbitmap-and-mfc-cstaticsetbitmap
 
Share this answer
 
Comments
SoMad 23-Jan-14 1:46am    
So SetBitmap() returns a handle to the previous bitmap loaded in the control and your fix is to do DeleteObject() on that if it is non-NULL, correct?
I think that is something that is easily overlooked and I will go through some of my projects to check if that is done consistently.

Soren Madsen
wy737 23-Jan-14 13:38pm    
HBITMAP prev = m_BitmapPreview.SetBitmap( hcurr ); // ---- without this line memory usage doesn't go up rapidly every second.
if (NULL != prev)
{
DeleteObject(prev); // *** This is very important !!!
}

// Later:
DeleteObject( hcurr ); // ---- returns non-zero which would point out that it was freed properly.

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