【发布时间】:2010-08-06 15:15:21
【问题描述】:
以下是我拥有的代码 sn-p:
// 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]; 返回一个自动释放的NSImage。我正在释放对象并且还有NSAutoreleasePool。但每次执行此代码 sn-p 时,它仍然会泄漏大约 3-4 MB 的内存。我不确定错误在哪里。
【问题讨论】:
-
TIFFRepresentation返回一个 NSData 对象。你为什么要从中创造另一个?此外,getCurrentFrameImage不应该使用单词get,它用于通过引用返回某些内容的方法;我建议像captureCurrentFrameImage。