Assertion in OpenGLContext when destroying parent component, even though destructor detaches context

I have an OpenGLContext that is attached to a component, let’s call it MyComponent.
This component takes care of attaching and detaching the OpenGLContext in its constructor and destructor, respectively.

When destroying the component that contains the MyComponent instance at the application’s shutdown, I’m hitting the following assertion in juce_OpenGLContext.cpp:740:

    void componentBeingDeleted (Component& c) override
    {
        /* You must call detach() or delete your OpenGLContext to remove it
           from a component BEFORE deleting the component that it is using!
        */
        jassertfalse;

        ComponentMovementWatcher::componentBeingDeleted (c);
    }

This doesn’t make a lot of sense to me, as the component c passed into this function is the parent component that contains the MyComponent which actually has the OpenGLContext bound. So why would the OpenGLContext complain here? Shouldn’t the assertion only be thrown when c is actually the component it’s bound to?

MyComponent's destructor is only reached after the assertion was made. Anything I can do to fix this? Is this correct behaviour?

I use something like this from the examples, I don’t know if it helps?

 void Panel::openGLContextClosing() override
	{
		shaders.clear();
		shape.reset();
		attributes.reset();
		uniforms.reset();
       	if (texture.getTextureID())
		{
			texture.release();
		}
	}

I also have

    OpenGLContext  openGLContext;
    Panel(UpStereroProAudioProcessor &p) : DSP(p)
	{
		openGLContext.setRenderer(this);
		openGLContext.attachTo(*this);
     .
     .
     .

    }
	~Panel()
	{
		openGLContext.detach();
    }

Thanks!

I do correctly free up any OpenGL resources in openGLContextClosing, that’s not the issue and not what triggers the assertion.

I also do detach the OpenGLContext in the destructor, just like you do - the only difference is that my OpenGLContext lives in a child component, not directly in the AudioProcessorEditor.

In my brief example, ‘Panel’ is a child of the editor which has openGLContext as a member variable.

class Panel :   public Component,
         		public OpenGLRenderer
{
  OpenGLContext  openGLContext;
 .
 .

}

I see. I have the same thing, wonder why it’s an issue with my code.

You could try commenting out as much as possible of associated code to narrow down what’s causing the problem?

I found the culprit when hunting down a different bug - I had a shared_ptr to the MyComponent (the one that owns the OpenGLContext) hanging around for longer than its parent component, so naturally the parent component’s destructor would be called before that of MyComponent.

Thanks for your help, Dave!