GdiplusStartup/Shutdown should not be called in DllMain


From GitHub Issue #180, submitted by @nextfullstorm:

There is an application hang on exit if you use MapWinGIS in environment where you cannot control GDI+ initialization/finalization. For example, Delphi project: if there is an unit with mawingis AND GDI+ code, than app will hang on shutdown forever. Why? Because Delphi calls GdiplusStartup/Shutdown automatically and mapwingis calls it from DllMain (its strictly prohibited in docs), which calls causes deadlock on second GdiplusShutdown.

Steps to reproduce the behavior:

Create Delphi project "VCL Forms Application". I tested it on Delphi 10.2.
Add GDIPObj to uses section of main form unit.
Drop a TmwMap component on main form and ensure that MapWinGIS_TLB added to uses section.
That's it!
Build project, run, close app. Delphi debugger show app still running. We can see call stack if we press Pause Debug.

Test environment: Delphi 10.2, MapWinGIS 5.1, Windows 8.1/10 x64.

I've read long comment about GdiplusStartup/Shutdown in Map.cpp:723. Clearly it not works as it states and NotificationHook/Unhook doesn't help here (at least for me). I was able to fix it by bringing back old solution (GdiplusStartup in CMapView ctor, GdiplusShutdown in CMapView dtor) AND adding TileCacheManager::CloseAll(); before GdiplusShutdown. Latter is for fixing "issues with tile cache" as long comment states.




Jerry Faust
November 27, 2019, 7:34 PM

GdiplusStartup/Shutdown calls have been moved from Dll's InitInstance/ExitInstance back to the CMapView constructor/destructor. This better follows Microsoft's recommendations, and allows for transparent initialization whether or not the calling environment does it's own statrup and shutdown. If any individual classes end up requiring GDI initialization outside of the map initialization, these can be handled on a case-by-case basis.


Jerry Faust


Jerry Faust



Fix versions