【问题标题】:writeImageToSavedPhotosAlbum leaks memory?writeImageToSavedPhotosAlbum 泄漏内存?
【发布时间】:2011-10-08 05:06:38
【问题描述】:

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

我有一个应用程序,它获取 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

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

非常感谢任何帮助!!!

【问题讨论】:

    标签: iphone memory-leaks assetslibrary


    【解决方案1】:

    我的代码和你的类似:

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

    【讨论】:

    • 嗨乔纳森 - 感谢您的回复。我尝试使我的库成为 ivar(我没有元数据)并且不在那里发布它,但不幸的是我没有骰子,同样的泄漏。我什至尝试保留我的 CGImage 而不释放它进行测试 - 仍然得到一般的块泄漏。无论如何感谢您的回复,也许这会帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2011-10-31
    • 2019-08-10
    • 2013-06-24
    • 2011-03-22
    • 2015-04-20
    • 2012-09-21
    相关资源
    最近更新 更多