title: GDI Resource Leak
body: <p>I have found a GDI leak in our huge application software.</p>
  
  <p>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.</p>
  
  <p>Even when I use "DeleteObject(bitmap)".</p>
  
  <p>Have I done some thing wrong ? 
  Do you have any thoughts?</p>
  
  <p>Thanks.</p>
  
  <pre><code>// 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()-&gt;m_hInstance,BEACON_BIG_RED_ON) ;
     m_static.SetBitmap (bitmap );
     DeleteObject(bitmap);
  
  
     // This is OK !!!
     bitmap = LoadBitmap ( AfxGetApp()-&gt;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
  }
  </code></pre>