writeImageToSavedPhotosAlbum 泄漏内存?
最近在这方面遇到了困难,希望有人能帮忙!刚开始发帖,但发现这是我帮助我使用应用程序的首选网站。
我有一个应用程序,它获取 CGImage 并使用 writeImageToSavedPhotosAlbum:orientation:completionBlock: 将其复制到照片库。功能上工作得很好,但仪器似乎告诉我有泄漏。通过反复试验和代码注释,我发现这条特定的行导致了泄漏:
[library writeImageToSavedPhotosAlbum:myCGImage
orientation:assetOrientation
completionBlock:^(NSURL *assetURL, NSError *error){
NSLog(@"image copied to album");
}];
该行对我来说似乎无害,所以我真的不确定为什么它会导致问题。注释掉了,没有泄露。把它留在里面,我看到泄漏了!
这是 Instruments 在 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 ,并将 library 作为好吧,assetOrientation 只是一个 ALAssetOrientation。没有什么是自定义代码,所以我很困惑! (如果这可能导致问题,我很乐意发布与此相关的其他代码行)。
非常感谢任何帮助!
Been having a tough time on this one, hope someone can help! New to posting, but have found this to be my go-to site for helping me through my apps.
I have an app that takes a CGImage and copies it to the Photo Library using writeImageToSavedPhotosAlbum:orientation:completionBlock:. Functionally works great, but Instruments seems to be telling me I have a leak. Through trial and error and commenting of code, I've found that this particular line is causing the leak:
[library writeImageToSavedPhotosAlbum:myCGImage
orientation:assetOrientation
completionBlock:^(NSURL *assetURL, NSError *error){
NSLog(@"image copied to album");
}];
That line seems innocuous enough to me, so I'm really not sure why it's causing a problem. Commented out, no leak. Leave it in, I see a leak!
Here's what Instrument shows in Leaked Blocks:
Leaked Object # Address Size Responsible Library Responsible Frame
GeneralBlock-36864, 0x8c77000 36.00 KB MusicLibrary MemNewPtrClear
Here's the stack trace from Instruments, which seems to imply it's related to the photo library indeed:
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
I do release myCGIImage later, and library as well, and assetOrientation is simply a ALAssetOrientation. Nothing else is custom code, so I'm stumped! (I'd be happy to post my other lines of code surrounding this if that may cause the problem).
Any help is hugely appreciated!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有与你类似的代码:
并且我看到了与你完全相同的泄漏:
使用堆栈:
我看不到代码有任何错误,但再次查看它似乎很愚蠢每次我想写入图像时都会分配 ALAssetsLibrary 和 NSMutableDictionary。我重新编写了代码来保留它们,并从完成块中删除了释放调用,泄漏神奇地消失了。
我不确定其中是否真的对您有很大帮助。但这确实让我想知道苹果的代码中是否真的存在问题,该问题仅在某些情况下才会被触发 - 当然我并没有看到我正在编写的每个图像的泄漏,只有其中一些。
I had similar code to yours:
And I was seeing exactly the same leak as you:
with a stack:
I can't see anything wrong with the code, but on looking at it again it seemed stupid to be allocing the ALAssetsLibrary and the NSMutableDictionary every time I wanted to write an image. I re-wrote the code to keep hold of them, and removed the release calls from the completion block, and the leak has magically gone away.
I'm not sure if any of that actually helps you very much. But it does make me wonder if there isn't actually a problem in Apple's code, which is only getting triggered in certain circumstances -- certainly I wasn't seeing a leak for every image I was writing, only some of them.