GDI+ initialization
-
I've got a class that wraps GDI+. In the constructor I call GdiplusStartup, and in the destructor I call GdiplusShutdown. This could result in multiple startup & shutdown calls. (And possibly several startup calls before any shutdown calls) Is it safe to make these calls multiple times?
-
I've got a class that wraps GDI+. In the constructor I call GdiplusStartup, and in the destructor I call GdiplusShutdown. This could result in multiple startup & shutdown calls. (And possibly several startup calls before any shutdown calls) Is it safe to make these calls multiple times?
-
indigox3 wrote:
Is it safe to make these calls multiple times?
What does the documentation say?
Not much relevant to my question. http://msdn2.microsoft.com/en-us/library/ms534077.aspx
-
I've got a class that wraps GDI+. In the constructor I call GdiplusStartup, and in the destructor I call GdiplusShutdown. This could result in multiple startup & shutdown calls. (And possibly several startup calls before any shutdown calls) Is it safe to make these calls multiple times?
I don't know. The docs state: "You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown." Although there's a token associated with each startup/shutdown pair, the token (or any other context) isn't used when working with GDI+ objects. That means there's no internal context relating created GDI+ objects with a GdiPlusStartup() call. If I had to guess, I'd say it will work fine - the DLL is probably released when the last token is returned to GdiPlusShutdown(). It's not documented, however, so that's a complete assumption, and relying on that assumption could be disastrous in the future.. To be safe, I personally chose (and still choose) to do this pair of calls ONCE for an entire application. This can be done with a singleton class wrapping the gdiplus dll (may fit nice with your wrapper class), or simply calling the startup/shutdown functions during app initialization/cleanup. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I've got a class that wraps GDI+. In the constructor I call GdiplusStartup, and in the destructor I call GdiplusShutdown. This could result in multiple startup & shutdown calls. (And possibly several startup calls before any shutdown calls) Is it safe to make these calls multiple times?
Thanks for the question, I did not know the answer and I was curious myself so I took the liberty of investigating. My first clue was in the MSDN It says: Each call to GdiplusStartup should be paired with a call to GdiplusShutdown. http://msdn2.microsoft.com/en-us/library/ms534076.aspx[^] So I fired up IDA Pro and began dissassembling gdiplus.dll to have a look at the op-codes. I always want to know whats really going on. Indeed, it appears each call to GdiplusStartup results in the critical section wrapped incrementing of a reference counter. Subsequently each call to GdiplusShutdown performs the opposite with the decrementing of the reference counter dword_7A20A040. Cleanup of the objects does not appear to take place until the reference count is zero. So it seems it is indeed safe to call GdiplusStartup/GdiplusShutdown in parallel between threads, and even within the same thread. As long as each GdiplusStartup is followed by a GdiplusShutdown. Best Wishes, -Randor (David Delaune)
-
Thanks for the question, I did not know the answer and I was curious myself so I took the liberty of investigating. My first clue was in the MSDN It says: Each call to GdiplusStartup should be paired with a call to GdiplusShutdown. http://msdn2.microsoft.com/en-us/library/ms534076.aspx[^] So I fired up IDA Pro and began dissassembling gdiplus.dll to have a look at the op-codes. I always want to know whats really going on. Indeed, it appears each call to GdiplusStartup results in the critical section wrapped incrementing of a reference counter. Subsequently each call to GdiplusShutdown performs the opposite with the decrementing of the reference counter dword_7A20A040. Cleanup of the objects does not appear to take place until the reference count is zero. So it seems it is indeed safe to call GdiplusStartup/GdiplusShutdown in parallel between threads, and even within the same thread. As long as each GdiplusStartup is followed by a GdiplusShutdown. Best Wishes, -Randor (David Delaune)
Cool! Thanks for that analysis :) Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Thanks for the question, I did not know the answer and I was curious myself so I took the liberty of investigating. My first clue was in the MSDN It says: Each call to GdiplusStartup should be paired with a call to GdiplusShutdown. http://msdn2.microsoft.com/en-us/library/ms534076.aspx[^] So I fired up IDA Pro and began dissassembling gdiplus.dll to have a look at the op-codes. I always want to know whats really going on. Indeed, it appears each call to GdiplusStartup results in the critical section wrapped incrementing of a reference counter. Subsequently each call to GdiplusShutdown performs the opposite with the decrementing of the reference counter dword_7A20A040. Cleanup of the objects does not appear to take place until the reference count is zero. So it seems it is indeed safe to call GdiplusStartup/GdiplusShutdown in parallel between threads, and even within the same thread. As long as each GdiplusStartup is followed by a GdiplusShutdown. Best Wishes, -Randor (David Delaune)
-
I don't know. The docs state: "You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown." Although there's a token associated with each startup/shutdown pair, the token (or any other context) isn't used when working with GDI+ objects. That means there's no internal context relating created GDI+ objects with a GdiPlusStartup() call. If I had to guess, I'd say it will work fine - the DLL is probably released when the last token is returned to GdiPlusShutdown(). It's not documented, however, so that's a complete assumption, and relying on that assumption could be disastrous in the future.. To be safe, I personally chose (and still choose) to do this pair of calls ONCE for an entire application. This can be done with a singleton class wrapping the gdiplus dll (may fit nice with your wrapper class), or simply calling the startup/shutdown functions during app initialization/cleanup. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I don't know. The docs state: "You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown." Although there's a token associated with each startup/shutdown pair, the token (or any other context) isn't used when working with GDI+ objects. That means there's no internal context relating created GDI+ objects with a GdiPlusStartup() call. If I had to guess, I'd say it will work fine - the DLL is probably released when the last token is returned to GdiPlusShutdown(). It's not documented, however, so that's a complete assumption, and relying on that assumption could be disastrous in the future.. To be safe, I personally chose (and still choose) to do this pair of calls ONCE for an entire application. This can be done with a singleton class wrapping the gdiplus dll (may fit nice with your wrapper class), or simply calling the startup/shutdown functions during app initialization/cleanup. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
The docs state:
Seems pretty clear to me :rolleyes: "Each call to GdiplusStartup should be paired....."
GdiplusShutdown Function
The GdiplusShutdown function cleans up resources used by Microsoft Windows GDI+. Each call to GdiplusStartup should be paired with a call to GdiplusShutdown.
-
Not much relevant to my question. http://msdn2.microsoft.com/en-us/library/ms534077.aspx
indigox3 wrote:
Not much relevant to my question.
Seems pretty clear to me :rolleyes: "Each call to GdiplusStartup should be paired....."
GdiplusShutdown Function
The GdiplusShutdown function cleans up resources used by Microsoft Windows GDI+. Each call to GdiplusStartup should be paired with a call to GdiplusShutdown.