writeImageToSavedPhotosAlbum泄漏内存?

时间:2011-07-19 04:36:54

标签: iphone memory-leaks assetslibrary

在这个问题上度过了一段艰难时期,希望有人可以提供帮助!新发布,但发现这是我的首选网站,可以帮助我完成我的应用。

我有一个应用程序,它使用CGImage并使用writeImageToSavedPhotosAlbum将其复制到照片库:orientation:completionBlock:。功能上很棒,但乐器似乎告诉我,我有泄漏。通过反复试验和对代码的评论,我发现这条特定的行导致了泄漏:

[library writeImageToSavedPhotosAlbum:myCGImage 
                              orientation:assetOrientation
                          completionBlock:^(NSURL *assetURL, NSError *error){
                              NSLog(@"image copied to album");
                          }];

那条线对我来说似乎无害,所以我真的不确定它为什么会引起问题。注释掉,没有泄漏。留下来,我看到了泄漏!

这是仪器在泄漏块中显示的内容:

Leaked Object   #   Address Size    Responsible Library Responsible Frame
GeneralBlock-36864,     0x8c77000   36.00 KB    MusicLibrary    MemNewPtrClear

这是来自Instruments的堆栈跟踪,这似乎意味着它确实与照片库有关:

0 libsystem_c.dylib calloc
1 MusicLibrary MemNewPtrClear
2 MusicLibrary ReadITImageDB
3 MusicLibrary -[MLPhotoLibrary _loadImageLibrary]
4 MusicLibrary -[MLPhotoLibrary albums]
5 PhotoLibrary -[PLPhotoLibrary albums]
6 PhotoLibrary -[PLPhotoLibrary eventAlbumContainingPhoto:]
7 PhotoLibrary -[PLPhotoLibrary pictureWasTakenOrChanged]
8 PhotoLibrary __-[PLAssetsSaver queueJobData:requestEnqueuedBlock:completionBlock:imagePort:previewImagePort:]_block_invoke_2
9 libdispatch.dylib _dispatch_call_block_and_release
10 libdispatch.dylib _dispatch_main_queue_callback_4CF$VARIANT$up
11 CoreFoundation __CFRunLoopRun
12 CoreFoundation CFRunLoopRunSpecific
13 CoreFoundation CFRunLoopRunInMode
14 GraphicsServices GSEventRunModal
15 GraphicsServices GSEventRun
16 UIKit -[UIApplication _run]
17 UIKit UIApplicationMain
18 mogofoto main /Users/Jutsu/Documents/mogofoto2/main.m:14
19 mogofoto start

我稍后会发布 myCGIImage ,而 library 也是如此,而assetOrientation只是一个ALAssetOrientation。没有别的东西是自定义代码,所以我很难过! (如果可能导致问题,我很乐意发布我的其他代码行。)

非常感谢任何帮助!!!

1 个答案:

答案 0 :(得分:0)

我和你的代码类似:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];

/* ... set up the metadata */

[library writeImageToSavedPhotosAlbum:image.CGImage metadata:metadata
                      completionBlock:^(NSURL *assetURL, NSError *error)
 { NSLog(@"assetURL %@", assetURL);
     [metadata release];
     [library release];
 }
 ];

我看到与你完全相同的泄漏:

Leaked Object   Address Size    Responsible Library Responsible Frame
GeneralBlock-36864,0x5066000    36.00 KB    MusicLibrary    MemNewPtrClear
GeneralBlock-36864,0x4fd3000    36.00 KB    MusicLibrary    MemNewPtrClear
GeneralBlock-36864,0x4f72000    36.00 KB    MusicLibrary    MemNewPtrClear
GeneralBlock-36864,0x45ce000    36.00 KB    MusicLibrary    MemNewPtrClear

有一个堆栈:

  0 libsystem_c.dylib calloc
  1 MusicLibrary MemNewPtrClear
  2 MusicLibrary ReadITImageDB
  3 MusicLibrary -[MLPhotoLibrary _loadImageLibrary]
  4 MusicLibrary -[MLPhotoLibrary albums]
  5 PhotoLibrary -[PLPhotoLibrary albums]
  6 PhotoLibrary -[PLPhotoLibrary eventAlbumContainingPhoto:]
  7 PhotoLibrary -[PLPhotoLibrary pictureWasTakenOrChanged]
  8 PhotoLibrary __-[PLAssetsSaver queueJobData:requestEnqueuedBlock:completionBlock:imagePort:previewImagePort:]_block_invoke_2
  9 libdispatch.dylib _dispatch_call_block_and_release
 10 libdispatch.dylib _dispatch_main_queue_callback_4CF$VARIANT$up
 11 CoreFoundation __CFRunLoopRun
 12 CoreFoundation CFRunLoopRunSpecific
 13 CoreFoundation CFRunLoopRunInMode
 14 GraphicsServices GSEventRunModal
 15 GraphicsServices GSEventRun
 16 UIKit -[UIApplication _run]
 17 UIKit UIApplicationMain
 18 myAppName main
 19 myAppName start

我在代码中看不到任何错误的,但是再次查看它时,每次我想要编写图像时分配ALAssetsLibrary和NSMutableDictionary似乎都很愚蠢。我重新编写代码以保持它们,并从完成块中删除释放调用,并且泄漏已经神奇地消失了。

我不确定是否有任何帮助你。但它确实让我想知道Apple的代码中是否实际上没有问题,这只会在某些情况下被触发 - 当然我没有看到我写的每个图像的泄漏,只有其中一些。