1

在这方面遇到了困难,希望有人能提供帮助!刚开始发帖,但发现这是我的首选网站,可以帮助我完成我的应用程序。

我有一个应用程序,它采用 CGImage 并使用 writeImageToSavedPhotosAlbum:orientation:completionBlock: 将其复制到照片库。功能上效果很好,但 Instruments 似乎告诉我我有泄漏。通过反复试验和代码注释,我发现这个特定的行导致了泄漏:

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

那条线对我来说似乎无害,所以我真的不确定它为什么会导致问题。注释掉,没有泄漏。把它留在里面,我看到一个泄漏!

以下是 Instrument 在 Leaked Blocks 中显示的内容:

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,assetOrientation 只是一个 ALAssetOrientation。没有其他东西是自定义代码,所以我很难过!(如果这可能导致问题,我很乐意发布我的其他代码行)。

非常感谢任何帮助!!!

4

1 回答 1

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 的代码中是否真的存在问题,它只会在某些情况下被触发——当然我并没有看到我写的每张图片都有泄漏,只有其中一些。

于 2011-07-24T11:28:43.123 回答