Memory Management for Plug-ins that Work with Bitmaps
 
 
 

Bitmap Memory Management

Memory is allocated and de-allocated by the bitmap classes in various ways. It is important to understand when memory needs to be freed by the developer, and when the system will take care of freeing it. This section discusses this issue.

The bitmap manager methods BitmapManager::Create() and BitmapManager::Load() both return pointers to bitmaps that need to be freed when the developer is done working with them. This is accomplished by simply using the delete operator on the pointer returned from the methods. The pseudo code below shows both of these cases.

This is the BitmapManager::Create() case:

Bitmap *bmap;
BitmapInfo bi;
 
bmap = TheManager->Create(&bi);
// Work with the bitmap...
// ...
// Free the bitmap when done
if (bmap) bmap->DeleteThis();

This is the BitmapManager::Load() case:

BMMRES status;
Bitmap *bmap;
BitmapInfo bi;
 
BOOL res = TheManager->SelectFileInput(&bi, ip->GetMAXHWnd(), _T("Open File"));
bmap = TheManager->Load(&bi, &status);
// Work with the bitmap...
// ...
// Free the bitmap when done
if (bmap) bmap->DeleteThis();

As the code above shows, the developer is responsible for freeing memory from both these methods.

Another example of getting a pointer to a bitmap and needing to free it is the pointer returned from the method Bitmap::ToDib(). This method creates a new Windows Device Independent Bitmap (DIB) and returns a pointer to it. The DIB bitmap is created from the bitmap whose method is called. The DIB is allocated internally using Bitmap::LocalAlloc() and must be freed by the developer using Bitmap::LocalFree(). The pseudo-code below show how this is done.

PBITMAPINFOpDib;
 
pDib = bmap->ToDib();
// Work with the bitmap...
// ...
// Free the bitmap when done
LocalFree(pDib);

There is a different case where the developer receives a pointer to a bitmap, but is not responsible for deleting it when done. If you have a Image Filter plug-in, it receives a pointer to the video post bitmap queue. This bitmap should not be deleted as the video post system is using it internally. For example, in the ImageFilter::Render() method, the filter plug-in has access to a source bitmap named srcmap. The methods of this pointer may be called, but the bitmap itself should not be deleted by the plug-in. Video post will take care of it.