NSImage 和相关 API 泄​​漏内存

发布于 2024-09-13 01:46:21 字数 1247 浏览 6 评论 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]; 返回一个自动释放的 NSImage。我正在释放对象,并且还有 NSAutoreleasePool 就位。但每次执行此代码片段时,它仍然会泄漏大约 3-4 MB 的内存。我不确定错误在哪里。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2024-09-20 01:46:21

您可以通过使 captureCurrentFrameImage 返回 NSBitmapImageRep 而不是 NSImage 来大大简化此代码,因为您实际上从未在此处使用 NSImage 进行任何操作。必要时,您可以将图像代表包装在图像中,对于此代码,只需使用图像代表本身即可生成 PNG 数据。除此之外,这还可以帮助您节省 TIFF 表示形式的麻烦。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文