Question

Following is the code snippet that I have:

 // Make Auto release pool
 NSAutoreleasePool * autoReleasePool = [[NSAutoreleasePool alloc] init];
 try
 {
  if (mCapture)
  {
   // Get the image reference
   NSImage* image = NULL;
   image = [mCapture getCurrentFrameImage];

   // Get the TIFF data
   NSData *pDataTifData = [[NSData alloc] initWithData:[image TIFFRepresentation]]; 
   NSBitmapImageRep *pBitmapImageRep = [[NSBitmapImageRep alloc] initWithData:pDataTifData];

   // Convert to BMP data
   NSData *pDataBMPData; 
   pDataBMPData = [pBitmapImageRep representationUsingType: NSPNGFileType
               properties: nil];

   // Save to specified path
   ASL::String strPath =  ASL::MakeString(capInfo->thefile.name);
   NSString* pPath = (NSString*)ASL::MakeCFString(strPath);
   [pDataBMPData writeToFile:pPath
         atomically: YES];

   ::CFRelease(pPath);
   pDataBMPData = nil;

   [pBitmapImageRep release];
   pBitmapImageRep = nil;
   [pDataTifData release];
   pDataTifData = nil;

   image = nil;
  }
 }
catch(...)
{
}
[autoReleasePool drain];

Note that image = [mCapture getCurrentFrameImage]; is returning an autoreleased NSImage. I am releasing objects and also have NSAutoreleasePool in place. But still it is leaking about 3-4 MB of memory everytime this code snippet is executed. I am not sure where the mistake is.

Was it helpful?

Solution

You could simplify this code a lot by making captureCurrentFrameImage return an NSBitmapImageRep instead of an NSImage, since you never actually use an NSImage for anything here. You can wrap the image rep in an image when necessary, and for this code, simply use the image rep by itself to produce the PNG data. Among other things, this saves you a trip through the TIFF representation.

If it still leaks after you make those changes, run your app under Instruments's Leaks template; the two instruments in that template, Leaks and ObjectAlloc, will help you hunt down whatever leaks you have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top