OpenGLContext memory trashing problem

The story starts with a nasty bug I ran into in my app yesterday. The symptom: OpenGLContext::CachedImage::initialiseOnThread() getting called with a null CachedImage object. Unfortunately for the purposes of tracking it down, this only happened in release builds.

Finally it looks like I have a symptom that can be reproduced easily in the Juce Demo.

  • Download Juce tip
  • In juce_OpenGL_osx.h, Replace the implementation of OpenGLContext::NativeContext::makeActive() with the following:

[code] bool makeActive() const noexcept
jassert (renderContext != nil);

    if ([renderContext view] != view)
        [renderContext setView: view];

printf("***** test1: %x \n", (unsigned long)this); // TEMP

    ThreadSafeNSOpenGLViewClass::makeActive (view);		

printf("***** test2: %x \n", (unsigned long)this); // TEMP

   return true;


  • do a Release build of Juce Demo.
  • run the demo
  • switch to OpenGL rendering.

I see many copies of

***** test1: 726a30 ***** test2: 0

Possibly indicating the stack is being trashed in makeActive().

Ok… Interesting.

The only thing I can see that might be relevant is a slightly dodgy-looking cast at line 57. You could try changing the BOOL in that line to a void*:

if (getIvar<void*> (self, "needsUpdate"))

…and see if that makes a difference.

This fixed the symptom. As does removing the call to getIvar(). So circumstantially, I’d say we have a fix. I suggest using addIvar (“needsUpdate”); etc.

Thanks, I’ll check in a fix.

Thanks for the speedy help.