views:

185

answers:

1

If I have a CImageList object (a simple wrapper around a HIMAGELIST), and I call:

m_pImageList->Replace(...);

http://msdn.microsoft.com/en-us/library/k10cwcdb.aspx

Who is responsible for clearing up the memory? Does the image list create a copy of any bitmap I pass in (i.e. can I create a CBitmap object on the stack then pass the address of this to the function)? Or must I create it on the heap and remember to manually free all the memory when the image list is destroyed?

MSDN isn't very clear on the subject of who's responsible.

+2  A: 

As you say, CImageList is a simple wrapper around an HIMAGELIST. CImageList::Replace just calls ImageList_Replace, whose documentation states:

The ImageList_Replace function copies the bitmap to an internal data structure. Be sure to use the DeleteObject function to delete hbmImage and hbmMask after the function returns.

This means you can create a CBitmap object on the stack, call CImageList::Replace, and the image list will take a copy of the bitmap from the CBitmap object. Then, when the CBitmap object goes out of scope, its destructor (or rather, the CGdiObject base class destructor) will call DeleteObject to delete the bitmap. Because the image list copies any bitmaps added to it, it should delete the bitmap that was replaced for you.

When the CImageList destructor executes, this calls ImageList_Destroy to delete the underlying HIMAGELIST, which should delete all the bitmaps owned by the image list.

ChrisN