Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Gdiplus::Bitmap destruction crash

Gdiplus::Bitmap destruction crash

Scheduled Pinned Locked Moved Graphics
graphicsquestionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alain Rist
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • A Alain Rist

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Why is drawing code getting called during destruction? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        Why is drawing code getting called during destruction? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        A Offline
        A Offline
        Alain Rist
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • A Alain Rist

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Cool :) Thanks for the update! Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups