Gdiplus::Bitmap destruction crash
-
Hi, I have a drawing routine in a class, two different object instances are alive:
void MyClass::DrawThumb(HDC hdc, RECT rc)
{
static Gdiplus::Bitmap bmThumb(m_hResource, MAKEINTRESOURCE(IDB_THUMB));
Gdiplus::Graphics gr(hdc);gr.DrawImage(&bmThumb, rc.left, rc.top);
}
This compiles (VS2008) and runs OK under Vista but crashes on some Vista systems when the final destruction is happening i.e. end of _crtMain(). What is the problem? Thanks, AR
-
Hi, I have a drawing routine in a class, two different object instances are alive:
void MyClass::DrawThumb(HDC hdc, RECT rc)
{
static Gdiplus::Bitmap bmThumb(m_hResource, MAKEINTRESOURCE(IDB_THUMB));
Gdiplus::Graphics gr(hdc);gr.DrawImage(&bmThumb, rc.left, rc.top);
}
This compiles (VS2008) and runs OK under Vista but crashes on some Vista systems when the final destruction is happening i.e. end of _crtMain(). What is the problem? Thanks, AR
Why is drawing code getting called during destruction? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Why is drawing code getting called during destruction? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks Mark, I got the answer with Jim Barry's help.
Mark Salsbery wrote:
Why is drawing code getting called during destruction?
The
static Gdiplus::Bitmap bmThumb
destructor runs after GdiplusShutdown has been called, resulting in undefined behaviour. To use this kind of construct I have to GdiplusStartup() and GdiplusShutdown() inside a static object instanciated at the very beginning of app creation, and deleted at the very end for instance:// main.cpp
#include "stdafx.h"class GdiPlusUser
{
ULONG _Token;
public:
GdiPlusUser() : _Token(0)
{
Gdiplus::GdiplusStartupInput input;
VERIFY(Gdiplus::GdiplusStartup(&_Token, &input, NULL) == Gdiplus::Ok);
}~GdiPlusUser() { ASSERT(\_Token); Gdiplus::GdiplusShutdown(\_Token); }
} _GdiPlusUser;
// anything else
Then no more crash, on XP or Vista. cheers, AR
modified on Tuesday, July 29, 2008 12:15 AM
-
Thanks Mark, I got the answer with Jim Barry's help.
Mark Salsbery wrote:
Why is drawing code getting called during destruction?
The
static Gdiplus::Bitmap bmThumb
destructor runs after GdiplusShutdown has been called, resulting in undefined behaviour. To use this kind of construct I have to GdiplusStartup() and GdiplusShutdown() inside a static object instanciated at the very beginning of app creation, and deleted at the very end for instance:// main.cpp
#include "stdafx.h"class GdiPlusUser
{
ULONG _Token;
public:
GdiPlusUser() : _Token(0)
{
Gdiplus::GdiplusStartupInput input;
VERIFY(Gdiplus::GdiplusStartup(&_Token, &input, NULL) == Gdiplus::Ok);
}~GdiPlusUser() { ASSERT(\_Token); Gdiplus::GdiplusShutdown(\_Token); }
} _GdiPlusUser;
// anything else
Then no more crash, on XP or Vista. cheers, AR
modified on Tuesday, July 29, 2008 12:15 AM
Cool :) Thanks for the update! Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: