About recent coreGraphics fix

I have extremely rare crashes, but they all share the basically pattern, while a graphics context is created on a image on macOS (message Thread)

The crash either happens while creating the context

|0   libobjc.A.dylib               |0x00007fff7a8528bf objc_retain + 31|
|---|---|
|1   com.apple.CoreGraphics        |0x00007fff4eb05867 CGColorRetain + 22|
|2   com.apple.CoreGraphics        |0x00007fff4eb06e1b CGGStateCreateCopy + 68|
|3   com.apple.CoreGraphics        |0x00007fff4eb06dc1 CGGStackSave + 47|
|4   com.apple.CoreGraphics        |0x00007fff4eb06d72 CGContextSaveGState + 32|
|5   com.my.plugin        |0x0000000124de9109 juce::CoreGraphicsContext::CoreGraphicsContext(CGContext*, float, float) + 473 (juce_mac_CoreGraphicsContext.mm:245)|

or while drawing on it

|0   libsystem_pthread.dylib       |0x00007fff7bb2111b pthread_mutex_lock + 0|
|---|---|
|1   com.apple.CoreGraphics        |0x00007fff4eb06ea2 CGClipStackRetain + 14|
|2   com.apple.CoreGraphics        |0x00007fff4eb06e74 CGGStateClipCopy + 22|
|3   com.apple.CoreGraphics        |0x00007fff4eb06e2a CGGStateCreateCopy + 83|
|4   com.apple.CoreGraphics        |0x00007fff4eb06dc1 CGGStackSave + 47|
|5   com.apple.CoreGraphics        |0x00007fff4eb06d72 CGContextSaveGState + 32|
|6   com.my.plugin        |0x0000000122920b78 juce::CoreGraphicsContext::drawImage(juce::Image const&, juce::AffineTransform const&, bool) + 168 (juce_mac_CoreGraphicsContext.mm:571)

The crash happens always with a debug version, but without debugging it.

I use a juce version, right before this commit.

I now the question is stupid, but the crash is so rare that I cannot make a good evaluation.
Is it likely that such crashes were caused by the issue fixed in the commit?

okay, I finally found a way to reproduce the issues. But it seams the commit does not fix the issue here, but it looks like this a releated issue. I create images on a background thread, and show them on the message thread, sometimes also modifiy them on the message thread.
This was never a problem until now.

Logic Pro X(6966,0x700008dde000) malloc: Heap corruption detected, free list is damaged at 0x6000035434f0

***** Incorrect guard value: 209653962716399**

Okay, I guess I found the problem.

I have a thread which is painting on an image, and pushes it to message thread (via a fifo)

ScopedPointer<Image> img(createImage());
Graphics g(*img);
internal.paint(g,width,height,nullptr);
pushToMessageThread(img.release());

When in the same moment, on the message-thread another context is opened for the same image (while the context is still attached), it will corrupt the heap. (Which is no problem for the software renderer, but a big problem for CoreGraphics)

Solution was pretty simple

ScopedPointer<Image> img(createImage);
{
  Graphics g(*img);
  internal.paint(g,width,height,nullptr);
} 
pushToMessageThread(img.release());

There should be a warning if you attach more then one context to one CoreGraphicsImage, because the errors that arise do not allow conclusions about what the actual problem is.

I’m not sure how we can police that. If there are no threads involved then having multiple contexts open should be fine (and people may be doing this in their existing products).