Any suggestions for crashes in PHCoreImageManager

Hi all,


Bit of a plea for help with our current top crash issue in a Photos/CoreData/networked application.


Essentially all the stack traces originate in -[PHCoreImageManager _schedulePurgeForCPLPrefetching:domain:onMainQueue:didCancelHandler:]. Some examples:


EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000010
Crashed: Thread
0  CoreFoundation                 0x1843d3920 -[__NSSetM addObject:] + 288
1  Photos                         0x18db5f8ec -[PHCoreImageManager _schedulePurgeForCPLPrefetching:domain:onMainQueue:didCancelHandler:] + 160
2  Photos                         0x18db5fb6c -[PHCoreImageManager _cancelAndFlushPreheatWithAsset:preheatItem:domain:operation:didCancelHandler:] + 268
3  Photos                         0x18db6020c -[PHCoreImageManager _cancelAndFlushPreheatWithAsset:format:CPLPrefetching:domain:operation:outPreheatItem:didCancelHandler:] + 176
4  Photos                         0x18db7f8a8 __84-[PHCachingImageManager startCachingImagesForAssets:targetSize:contentMode:options:]_block_invoke1872 + 688
5  Photos                         0x18db809bc __84-[PHCachingImageManager startCachingImagesForAssets:targetSize:contentMode:options:]_block_invoke_21918 + 368
6  Photos                         0x18db7c17c __107-[PHCachingImageManager _fireCloudDownloadOfImageForAsset:format:optimalSourcePixelSize:completionHandler:]_block_invoke_3 + 348
7  PhotoLibraryServices           0x18d33a70c __58-[PLPreheatItem startPreheatRequestWithCompletionHandler:]_block_invoke_252 + 80
8  AssetsLibraryServices          0x18cb4f240 __pl_dispatch_async_block_invoke + 36
9  libdispatch.dylib              0x183f39630 _dispatch_call_block_and_release + 24
10 libdispatch.dylib              0x183f395f0 _dispatch_client_callout + 16
11 libdispatch.dylib              0x183f45634 _dispatch_queue_drain + 864
12 libdispatch.dylib              0x183f3d0f4 _dispatch_queue_invoke + 464
13 libdispatch.dylib              0x183f47504 _dispatch_root_queue_drain + 728
14 libdispatch.dylib              0x183f47224 _dispatch_worker_thread3 + 112
15 libsystem_pthread.dylib        0x18414d470 _pthread_wqthread + 1092
16 libsystem_pthread.dylib        0x18414d020 start_wqthread + 4


*** error for object 0x13ffd14a0: pointer being freed was not allocated

Crashed: PLPreheatItem

0 libsystem_kernel.dylib 0x180d0411c __pthread_kill + 8

1 libsystem_pthread.dylib 0x180dd0ef8 pthread_kill + 112

2 libsystem_c.dylib 0x180c75dac abort + 140

3 libsystem_malloc.dylib 0x180d30374 szone_size + 522

4 CoreFoundation 0x18117874c __rehashs + 200

5 Photos 0x18ae5cc00 -[PHCoreImageManager _schedulePurgeForCPLPrefetching:domain:onMainQueue:didCancelHandler:] + 160

6 Photos 0x18ae5ce80 -[PHCoreImageManager _cancelAndFlushPreheatWithAsset:preheatItem:domain:operation:didCancelHandler:] + 268

7 Photos 0x18ae5d520 -[PHCoreImageManager _cancelAndFlushPreheatWithAsset:format:CPLPrefetching:domain:operation:outPreheatItem:didCancelHandler:] + 176

8 Photos 0x18ae7bfc4 __84-[PHCachingImageManager startCachingImagesForAssets:targetSize:contentMode:options:]_block_invoke1885 + 688

9 Photos 0x18ae7d0d8 __84-[PHCachingImageManager startCachingImagesForAssets:targetSize:contentMode:options:]_block_invoke_21931 + 368

10 Photos 0x18ae787ec __107-[PHCachingImageManager _fireCloudDownloadOfImageForAsset:format:optimalSourcePixelSize:completionHandler:]_block_invoke_3 + 348

11 PhotoLibraryServices 0x18a5a7720 __58-[PLPreheatItem startPreheatRequestWithCompletionHandler:]_block_invoke_252 + 80

12 AssetsLibraryServices 0x189d94430 __pl_dispatch_async_block_invoke + 36

13 libdispatch.dylib 0x180bb54bc _dispatch_call_block_and_release + 24

14 libdispatch.dylib 0x180bb547c _dispatch_client_callout + 16

15 libdispatch.dylib 0x180bc14c0 _dispatch_queue_drain + 864

16 libdispatch.dylib 0x180bb8f80 _dispatch_queue_invoke + 464

17 libdispatch.dylib 0x180bc3390 _dispatch_root_queue_drain + 728

18 libdispatch.dylib 0x180bc30b0 _dispatch_worker_thread3 + 112

19 libsystem_pthread.dylib 0x180dcd470 _pthread_wqthread + 1092

20 libsystem_pthread.dylib 0x180dcd020 start_wqthread + 4



From what I can see, most (if not all) the examples feature the PHImageManager class hierachy in two threads simultaneously. We have a few separate instances of PHImageManager and PHCachingImageManager concurrently, and I have reason to suspect that instances have underlying shared data and so it might not actually be safe to use multiple instances. It most cases it is as follows:


  1. Image manager responsible for delivering images for "processing". Certainly here we *don't* want caching.
  2. Caching Image manager responsible for delivering thumbnails for a collection view grid (using "pre-heating", as featured in the stack traces)
  3. Image manager responsible for delivering images for the search feature.


I have already looked into combining these for better safety, but this would be quite a big change, but something we could do if necessary. The isn't, however, anything in the documentation to suggest that what we're doing is unsafe, but the number of crashes would suggest that something is definitely wrong.


This is a bit of a shot in the dark, however, and I'm hoping that this means something to someone who might be able to suggest what we might be doing that is dangerous.


Of course please let me know if I can provide more information.


Many thanks

Replies

I have almost the same issue. Did you learn any more, or find a fix?

thanks

I'm having the same issue. Unpredictable random crashes.

I see this in a crashlog from the wild:


*** error for object 0x1580bfeb0: double free
CRASH_INFO_ENTRY_0
abort() called

This makes me think that it's not so much multi instances that's the issue, rather, that using it from multiple threads is the issue. I have older releases that don't crash nearly as much, and they dispatch_async to a serial queue to avoid concurrent issues. My newer versions don't have the serial queue anymore because I was using that for something I was doing (that I stopped).


I'm going to put back the serial queue for all calls to startCachingImagesForAssets and stopCachingImagesForAssets and see if that helps.

Hey, I had the same problem and guessed it to be a multithread issue, so when popping up the photos gallery I added dispatch_async to main thread, as such:


dispatch_async(dispatch_get_main_queue(), ^{

[viewController presentViewController:imagePicker animated:YES completion:nil];

self.viewControllerForNAvigation = viewController;

});


Issue has not returned yet, however since it's random - I don't know if it was solved or not.


What do you guys think?