OpenGL OgreWin32Window.cpp has a dangling pointer

Description

On Windows, the OpenGL rendersystem may cause a segmentation fault if the user attempts to use the render window after it is closed via the close button on the window.

In the OpenGL subsystem, there is the following code to delete the OpenGL context when the window is destroyed:

RenderSystems/GL/src/OgreWin32Window.cpp, Line 623 [in Win32Window::destroy()]:

delete mContext;

However, the member variable mContext is never actually set to 0, just deleted. This allows the (now deleted) OpenGL context pointer to leak out via getCustomAttribute():

RenderSystems/GL/src/OgreWin32Window.cpp, Line 878 [in Win32Window::getCustomAttribute(const String&,void*)]:

static_cast<GLContext*>(pData) = mContext;

The OpenGL RenderSystem itself properly checks against a null context, but since the context is never set to 0, it allows a destroyed window context to be passed to _switchContext, which in turn causes a segmentation fault.

RenderSystems/GL/src/OgreGLRenderSystem.cpp, Line 3600 [in GLRenderSystem::_setRenderTarget(RenderTarget*)]:

// Switch context if different from current one
GLContext *newContext = 0;
target->getCustomAttribute(GLRenderTexture::CustomAttributeString_GLCONTEXT, &newContext);
if(newContext && mCurrentContext != newContext)
{
_switchContext(newContext);
}

I experienced this because the official CEGUI frame listener does not check if the render target has been closed, so if someone closes the OpenGL window via clicking the close box on the window, the frame listener tries to render one last frame which causes it to switch context to the destroyed window and segmentation fault.

While i do think CEGUI should probably check if the window is closed in their frame listener, I think Ogre should also correctly clean up the context after it is destroyed.

The change I propose is to set mContext to 0 after it is deleted, which solves the problem in my testing.

Environment

Windows 7, Microsoft Visual Studio 10, OpenGL Renderer

Assignee

M

Reporter

Andrew McDowell

Labels

None

Time tracking

1h

Fix versions

Priority

Major
Configure