0

以下是我拥有的代码片段:

 // 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];

请注意,这image = [mCapture getCurrentFrameImage];是返回一个 autoreleased NSImage。我正在释放对象并且也已经NSAutoreleasePool到位。但每次执行此代码片段时,它仍然会泄漏大约 3-4 MB 的内存。我不确定错误在哪里。

4

1 回答 1

1

你可以通过captureCurrentFrameImage返回一个 NSBitmapImageRep 而不是 NSImage 来大大简化这段代码,因为你在这里从来没有真正使用过 NSImage。您可以在必要时将图像代表包装在图像中,对于此代码,只需使用图像代表本身来生成 PNG 数据。除其他外,这可以节省您通过 TIFF 表示的时间。

如果在您进行这些更改后仍然泄漏,请在 Instruments 的 Leaks 模板下运行您的应用程序;该模板中的两个工具 Leaks 和 ObjectAlloc 将帮助您找到任何泄漏。

于 2010-08-06T23:16:02.327 回答