Resource leak

I have some icons that I place in a new CImageList in the OnCreate in CMainFrame.  I the set several set them as icons to several of my buttons.  In the destructor for CMainFrame, I DeleteImageList() and then delete the CImageList.  When I close the program, Boundschecker tell me that I have resource leaks and tells me that I have to delete the icons that I set to the buttons so in OnClose for CMainFrame I tried to delete the icons like this for each button:
      CButton* btn;
      btn = (CButton*)m_wndMyCTRLBar.GetDlgItem( IDC_BUTTON1);

Boundschecker still says that I have the resource leaks.  Am I suppose to delete them from another event or am I deleting them incorrectly.  Please help end my confusion.
Who is Participating?
itsmeandnobodyelseConnect With a Mentor Commented:
How did you get the HICON handles for the bitmaps? Did you get them by a LoadIcon ? If so, Boundschecker may be wrong cause the resource handles came from the already loaded resource file and don't need to be freed. At least there is no FreeIcon function to call. If you load the icons from file the case is different.

Regards, Alex
jkrConnect With a Mentor Commented:
I'd rather suggest to do that in the destructor of your control bar. Apart from that, what does 'DestroyIcon()' return and what 'GetLastError()' if it returns FALSE?
ITSpgrmrAuthor Commented:
I did use LoadIcon and I was getting the icons from the resources and not from files.  So I don't have to worry about it since the resources in the resource file get dumped at the end.  That makes since and I like that answer.

DestroyIcon() was returning TRUE.

 I agree, taking care of the icons in the destructor of the toolbar is much better.  Sometimes looking back at my code, I wonder what I was thinking.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.