title: MFC Class attaches GDI bitmap. Should DeleteObject and Detach be used in the destructor?
body: <p>I have a class in MFC derived from CBitmap, named BitmapTools.</p>
  
  <p>In it, a function loads and attaches a Bitmap, as shown below.</p>
  
  <pre><code>bool BitmapTools::LoadAttachBitmap(LPCSTR bmpfile)
  {   
      H_Bitmap = (HBITMAP)LoadImage(NULL, bmpfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
      return Attach (H_Bitmap);   
  }
  </code></pre>
  
  <p>When the class goes out of scope and is destroyed, do I need to detach the bitmap and call DeleteObject, to free memory?</p>
  
  <p>I added the following in the destructor, but from what I have read, I am not sure if this is necessary.</p>
  
  <pre><code>BitmapTools::~BitmapTools()
  {
      this-&gt;Detach();
      DeleteObject();
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Hugh</p>