Non trivial bug on OpenGLContext with Component painting

I think I found a problem on OpenGLContext with shouldPaintComponent set to true.

Quick creation/destruction sometime perform a null pointer indirection (CachedImage threading made this bug non systematics)

OpenGLContext::setAssociatedObject assert at line 619 before crash

with nativeContext not null but c null.

Here a snippet helping you to reproduce the problem :

[code]// dummy renderer
class VoidRenderer : public OpenGLRenderer
{
virtual void newOpenGLContextCreated() {}
virtual void renderOpenGL() {}
virtual void openGLContextClosing() {}
};

// Ugly component owning renderer and context with public attach/detach.
class VoidRendererAndContextComponent : public Component
{
public:
VoidRendererAndContextComponent()
{glContext.setComponentPaintingEnabled(true); glContext.setRenderer(&renderer);}
~VoidRendererAndContextComponent()
{jassert(!glContext.isAttached())}
void attach()
{glContext.attachTo(*this);};
void detach()
{glContext.detach();}
private:
VoidRenderer renderer;
OpenGLContext glContext;
};

// Todo: add a Timer inheritance to a Component class of your choice and a startThread(100) when it’s visible.
// …

// Trick to ensure !MessageManagerLock::lockWasGained() succeed on CachedImage::run() :
// I call creation-attachement / detachement-deletion alternately in a timerCallback
virtual void timerCallback()
{
static bool mustCreateAndAttach = false; // static alternator
mustCreateAndAttach = !mustCreateAndAttach ;

static VoidRendererAndContextComponent* component = NULL; // ugly static just for testing

if (mustCreateAndAttach)
{
component = new VoidRendererAndContextComponent();
component->setSize(42, 42);
addAndMakeVisible(component);
component->attach();
}
else // detach and delete
{
component->detach();
removeChildComponent(component);
delete component;
}
}

//…
// end of your Component class…[/code]

Some additionnal infos for helping:

I’m running on Win7-64-SP1
Last Juce Git builded with Visual Studio 2008
Callstack :

juce::OpenGLContext::setAssociatedObject(name=0x0000000140fb3e58, newObject=0x0000000015f36250) Line 619 C++ juce::OpenGLContext::copyTexture'::`7'::OverlayShaderProgram::select(context={...}) Line 666 C++ juce::OpenGLContext::copyTexture(targetClipArea={...}, anchorPosAndTextureSize={...}, contextWidth=42, contextHeight=42) Line 731 C++ juce::OpenGLContext::CachedImage::paintComponent() Line 209 C++ juce::OpenGLContext::CachedImage::renderFrame() Line 158 C++ juce::OpenGLContext::CachedImage::run() Line 261 C++ juce::Thread::threadEntryPoint() Line 98 C++ juce::juce_threadEntryPoint(userData=0x0000000015f34d48) Line 106 C++ juce::threadEntryProc(userData=0x0000000015f34d48) Line 121 C++

Thanks! I’ve done some fixing - try it now!

Your fix seems correct this issue perfectly.

Thanks a lot for that !