unrecognized selector retainedCGImage


Jonathan Taylor
 

Hi all,

Can anybody advise on the following exception/crash that I am encountering? The issue is:
[NSWindowGraphicsContext retainedCGImage]: unrecognized selector sent to instance
The problem is that as far as I can see this is something internal to AppKit, and I’m not sure what is triggering this problem. This is running on OS 10.9.5 (yes, sorry…), built using Xcode 6.2, with both deployment target and SDK set to 10.9. Can anyone suggest what the problem is, or what I might do to diagnose it further?

Thanks
Jonny



Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSWindowGraphicsContext retainedCGImage]: unrecognized selector sent to instance 0x79828bf0'

Application Specific Backtrace 1:
0 CoreFoundation 0x9984e471 __raiseError + 193
1 libobjc.A.dylib 0x99329091 objc_exception_throw + 162
2 CoreFoundation 0x99852cb3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x9979e522 ___forwarding___ + 1010
4 CoreFoundation 0x9979e10e _CF_forwarding_prep_0 + 14
5 AppKit 0x903d7ed8 +[NSCGImageSnapshotRep _unlockFocusAndPerformBlockUsingCGImageAndCapturingContext:] + 129
6 AppKit 0x903d7e2b +[NSCGImageSnapshotRep unlockFocusCreatingCGImageSnapshotRep] + 138
7 AppKit 0x903d7d65 -[NSImage unlockFocus] + 204
8 Spim GUI 0x001416b3 -[GUIMovieBuilder updateCurrentFrameImage] + 1747
9 Spim GUI 0x00141977 -[GUIMovieBuilder observeValueForKeyPath:ofObject:change:context:] + 119
10 Foundation 0x932a39ef NSKeyValueNotifyObserver + 386
11 Foundation 0x932a2e6d NSKeyValueDidChange + 270
12 Foundation 0x933564f6 -[NSObject(NSKeyValueObserverNotification) didChange:valuesAtIndexes:forKey:] + 120
13 Foundation 0x93360934 -[NSKeyValueNotifyingMutableArray insertObject:atIndex:] + 184
14 AppKit 0x906a4572 -[NSArrayDetailBinder _performArrayBinderOperation:singleObject:multipleObjects:singleIndex:multipleIndexes:selectionMode:] + 1553
15 AppKit 0x906a48d0 -[NSArrayDetailBinder insertObjectIntoMasterArrayRelationship:atIndex:selectionMode:] + 78
16 AppKit 0x9069f1ac -[NSArrayController _insertObject:atArrangedObjectIndex:objectHandler:] + 190
17 AppKit 0x9069f562 -[NSArrayController insertObject:atArrangedObjectIndex:] + 441
18 AppKit 0x9069ef51 -[NSArrayController addObject:] + 160
19 Spim GUI 0x0013fbfa -[GUIMovieBuilder addSequenceForURL:] + 186
20 Spim GUI 0x000df4cc __36-[SpimApplication stopVideoSession:]_block_invoke + 460
21 Spim GUI 0x000e1533 __40+[ImageSaver afterAllBacklogCleared:do:]_block_invoke + 259
22 libdispatch.dylib 0x98c49386 _dispatch_client_callout + 50
23 libdispatch.dylib 0x98c50ddf _dispatch_source_latch_and_call + 150
24 libdispatch.dylib 0x98c4bcf8 _dispatch_source_invoke + 422
25 libdispatch.dylib 0x98c51a24 _dispatch_main_queue_callback_4CF + 207
26 CoreFoundation 0x997a7fae __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
27 CoreFoundation 0x9975f3c9 __CFRunLoopRun + 1897
28 CoreFoundation 0x9975e9ea CFRunLoopRunSpecific + 394
29 CoreFoundation 0x9975e84b CFRunLoopRunInMode + 123
30 HIToolbox 0x95850b5d RunCurrentEventLoopInMode + 259
31 HIToolbox 0x958508e2 ReceiveNextEventCommon + 526
32 HIToolbox 0x958506bd _BlockUntilNextEventMatchingListInModeWithFilter + 92
33 AppKit 0x902d4349 _DPSNextEvent + 1602
34 AppKit 0x902d3870 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 119
35 AppKit 0x902c615c -[NSApplication run] + 727
36 AppKit 0x902aeff8 NSApplicationMain + 1165
37 libdyld.dylib 0x90ece701 start + 1


Quincey Morris
 

On Feb 19, 2019, at 02:57 , Jonathan Taylor <jonathan.taylor@...> wrote:

The problem is that as far as I can see this is something internal to AppKit

I suggest that you start with most mundane possibility, that this is a zombie object.

My guess is that this is one of those crashes that can happen when breaking down a UI, such as when a window is closing. It’s easy to have a dangling reference to a weakly reference object that no longer exists.

This is based on seeing:

20  Spim GUI                            0x000df4cc __36-[SpimApplication stopVideoSession:]_block_invoke + 460

in the backtrace, trickling down to a KVO observer being notified:

9   Spim GUI                            0x00141977 -[GUIMovieBuilder observeValueForKeyPath:ofObject:change:context:] + 119

which then looks like it’s trying to *create* a NSImage:

8   Spim GUI                            0x001416b3 -[GUIMovieBuilder updateCurrentFrameImage] + 1747

which looks like it’s trying use some current window or view context:

5   AppKit                              0x903d7ed8 +[NSCGImageSnapshotRep _unlockFocusAndPerformBlockUsingCGImageAndCapturingContext:] + 129

If the window has already destroyed its view hierarchy, this might not work so well.

Unfortunately, this isn’t an easy kind of problem to debug. The answer is usually to nil some non-owning “delegate” reference somewhere, but finding where and when can be challenging.


Jon Gotow
 

On Feb 19, 2019, at 3:57 AM, Jonathan Taylor <jonathan.taylor@...> wrote:

The problem is that as far as I can see this is something internal to AppKit, and I’m not sure what is triggering this problem. This is running on OS 10.9.5 (yes, sorry…), built using Xcode 6.2, with both deployment target and SDK set to 10.9. Can anyone suggest what the problem is, or what I might do to diagnose it further?
As Quincey said, it looks like you've got a zombie pointer that's now pointing to an NSWindowGraphicsContext instead of the original, now-deallocated NSSnapshotBitmapGraphicsContext.

Turn on zombie objects in Xcode. In newer versions of Xcode, it's in the Run > Diagnostics for the project's scheme. I'm trying to remember where this is in Xcode 6 - I _think_ you want to launch the app using Instruments and then use the Zombie module in Instruments.

- Jon


Jon Gotow
 

On Feb 19, 2019, at 11:04 AM, Jon Gotow <gotow@...> wrote:

Turn on zombie objects in Xcode. In newer versions of Xcode, it's in the Run > Diagnostics for the project's scheme. I'm trying to remember where this is in Xcode 6 - I _think_ you want to launch the app using Instruments and then use the Zombie module in Instruments.
Oh - here you go. It's in the scheme's Run > Diagnostics tab even back in Xcode 6:

https://code.tutsplus.com/tutorials/what-is-exc_bad_access-and-how-to-debug-it--cms-24544

- Jon


Jonathan Taylor
 

Dear all,

Thanks for your replies. It turned out it wasn't due to a zombie pointer, though I did turn on that setting to check (hadn't thought of that as a possibility for this problem).

After quite a lot more poking around (much of it irrelevant in the end!) I realised that it was all down to the fact that under these specific circumstances the offending call to unlockFocus in my code was not preceded by a call to lockFocus. Fixing that bug has solved the problem! I think I was thrown off by the fact that this was only happening on one system and not reproducible on others - which, along with the ultimate crash being in private classes that I was struggling to find information on, primed me to think it was some sort of version-related issue.

Cheers
Jonny.