From 437377b5d50acd88639102adaf77fde7fd07184c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 9 Feb 2009 21:17:36 +0000 Subject: [PATCH] From Ralf Habacker, "on win32 there is a memory leak in recent svn code in GraphicsWindowWin32.cpp. in bool GraphicsWindowWin32::setWindow( HWND handle ) there is the following if/else statement if (_traits.valid() && _traits->setInheritedWindowPixelFormat) .... else setPixelFormat() _hglrc = ::wglCreateContext(...) [1] setPixelFormat() calls wglCreateContext() and saves the result into _hglrc which is overwritten by a second call to wglCreateContext() call at [1] The same behavior occurs in bool Win32WindowingSystem::getSampleOpenGLContext( OpenGLContext& context, HDC windowHDC, int windowOriginX, int windowOriginY ). The solution for this issue is to move the wglCreateContext() out of setPixelFormat() and to place it into the caller which is done to the appended file " --- src/osgViewer/GraphicsWindowWin32.cpp | 67 +++++++++++---------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/src/osgViewer/GraphicsWindowWin32.cpp b/src/osgViewer/GraphicsWindowWin32.cpp index 2bae6698f..9613b2a15 100644 --- a/src/osgViewer/GraphicsWindowWin32.cpp +++ b/src/osgViewer/GraphicsWindowWin32.cpp @@ -1172,6 +1172,20 @@ bool GraphicsWindowWin32::createWindow() return false; } + // + // Create the OpenGL rendering context associated with this window + // + + _hglrc = ::wglCreateContext(_hdc); + if (_hglrc==0) + { + reportErrorForScreen("GraphicsWindowWin32::createWindow() - Unable to create OpenGL rendering context", _traits->screenNum, ::GetLastError()); + ::ReleaseDC(_hwnd, _hdc); + _hdc = 0; + destroyWindow(); + return false; + } + Win32WindowingSystem::getInterface()->registerWindow(_hwnd, this); return true; } @@ -1209,37 +1223,23 @@ bool GraphicsWindowWin32::setWindow( HWND handle ) // Check if we must set the pixel format of the inherited window // - if (_traits.valid() && _traits->setInheritedWindowPixelFormat) + if (!setPixelFormat()) { - if (!setPixelFormat()) - { - reportErrorForScreen("GraphicsWindowWin32::setWindow() - Unable to set the inherited window pixel format", _traits->screenNum, ::GetLastError()); - _hdc = 0; - _hwnd = 0; - return false; - } + reportErrorForScreen("GraphicsWindowWin32::setWindow() - Unable to set the inherited window pixel format", _traits->screenNum, ::GetLastError()); + ::ReleaseDC(_hwnd, _hdc); + _hdc = 0; + _hwnd = 0; + return false; } - else + + _hglrc = ::wglCreateContext(_hdc); + if (_hglrc==0) { - // - // Create the OpenGL rendering context associated with this window - // - if (!setPixelFormat()) - { - reportErrorForScreen("GraphicsWindowWin32::setWindow() - Unable to set the inherited window pixel format", _traits->screenNum, ::GetLastError()); - _hdc = 0; - _hwnd = 0; - return false; - } - _hglrc = ::wglCreateContext(_hdc); - if (_hglrc==0) - { - reportErrorForScreen("GraphicsWindowWin32::setWindow() - Unable to create OpenGL rendering context", _traits->screenNum, ::GetLastError()); - ::ReleaseDC(_hwnd, _hdc); - _hdc = 0; - _hwnd = 0; - return false; - } + reportErrorForScreen("GraphicsWindowWin32::setWindow() - Unable to create OpenGL rendering context", _traits->screenNum, ::GetLastError()); + ::ReleaseDC(_hwnd, _hdc); + _hdc = 0; + _hwnd = 0; + return false; } if (!registerWindowProcedure()) @@ -1566,17 +1566,6 @@ bool GraphicsWindowWin32::setPixelFormat() return false; } - // - // Create the OpenGL rendering context associated with this window - // - - _hglrc = ::wglCreateContext(_hdc); - if (_hglrc==0) - { - reportErrorForScreen("GraphicsWindowWin32::setPixelFormat() - Unable to create OpenGL rendering context", _traits->screenNum, ::GetLastError()); - return false; - } - return true; }