views:

32

answers:

1

Hi

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. Please help.

thanks, SG

A: 

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.

Peter Hosey
I have already tried with instruments with ObjectAlloc and Memleak tools. They do not show any leaks while this code is executed but show peaks of 3-4 MB increase in memory allocations. All these peaks point to this code but no memory leak is shown by the tool. that is why I am at a loss here as everytime this code is executed, 3 MB memory is consumed but still not memory leak is shown by Instruments.[[NSBitmapImageRep alloc] initWithData:pDataTifData]; statement seems to be culprit but again I am not able to find any way to release the memory.
Sandeep
Performing the simplifications I suggested in my answer will reduce the memory usage of your application, since you'll be creating fewer objects. Does the memory usage keep going up and up and up, or does it go up and then back down?
Peter Hosey
I have removed the Tiff code and used the CVImageBuffer instead. But still it is leaking (as far I think unlike Instruments) in NSBitmapImageRep creation. Instruments shows many memory allocated blocks at this statement that is still living and this is 5MB in my case.
Sandeep