Sign in to follow this  
Guest HartmannH

GDI+ Cleanup Question

Recommended Posts

I've just recently started using GDI+ for a gauge. About 50% of the times, when I exit the simulator (in which the gauge works fine), I get a message about an unhandled exception in FS9.exe. Obviously, this is my gauge misbehaving, and I'm assuming it's something during cleanup. So 3 questions:----> Question 1. Is there anything more the following that needs to be done in order to properly clean up after using GDI+ functions?case PANEL_SERVICE_PRE_KILL:if (gdiPlusInitialized){ GdiplusShutdown(gdiplusToken); gdiPlusInitialized = false;}----> Question 2. I re-use quite a few variables, for example, in one case I declare an item:RectF clipRectF; // declare oncethen use this variable many times like this:clipRectF = RectF(...parameteres...);then later:clipRectF = RectF(...differentparameters...);Is this OK? Or am I leaking memory by doing this? Do these GDI+ objects need to be cleaned up afterwards in any way like we used to have to do in plain GDI32 with "DeleteObject", or is cleanup automatic?----> Question 3: Does the graphics.Dispose method need to be called? I've never seen this in any gauge examples, but it's in all the GDI+ examples in my book.

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Hi,on question 1: That's basically OK, but it's better if you move GdiplusStartup() to PANEL_SERVICE_CONNECT_TO_WINDOW and GdiplusShutdown() to PANEL_SERVICE_DISCONNECT so GDI+ is initialized only when the panel is loading and not everytime you switch between windowed and full screen mode. It's not a problem though.on question 2:you don't leak any memory by using a variable like this. A cleanup is only necessary if you use pointers: RectF *clipRectF = new RectF(...) must always be followed by delete clipRectF since the Managed Extensions for C++ (in VS.NET) are not available in gauges.Hope that helps.

Share this post


Link to post
Share on other sites

Thanks....I was pretty sure I was safe there since I wasn't using a pointer, but 10 years into C++ and I still screw 'em up!Still doesn't help explain the occasional unhandled exception....and I'm not even sure if "real people" would see the exception, or if it's only visible to me due to the presence of the development environment on my machine.

Share this post


Link to post
Share on other sites

Same here. 23 years into programming and you wouldn't believe how many stupid errors I make :-DI have no idea about that exception. Does it come up at a certain point? For example when unloading the aircraft?

Share this post


Link to post
Share on other sites

Geeeeeez, I found it, nevermind. I guess I have to admit that this was a stupid one too. I had totally forgotten that I was allocating some memory for a test string, but I never freed the pointer. And dummy me, I was allocating it at the top of the callback, not inside the deisred case, so it was getting allocated hundreds of times more than necessary, and never released.Funny, I was all worried about a memory leak with the way I was using GDI, and as it turns out, it was a memory leak using the most simplistic of pointers. Yikes, I must have been programming when I should have been getting some sleep!

Share this post


Link to post
Share on other sites

The frequency of the fs9.exe unhandled exception (during aircraft unload and/or simulator shutdown) error was reduced by my pointer correction, but hadn't gone completely away until I did as you had suggested, moving the GDI startup code into PANEL_SERVICE_CONNECT_TO_WINDOW and the shutdown code into PANEL_SERVICE_DISCONNECT. We'll see if that holds true, but for now, I've started and shutdown 50-60 times, and no crash.

Share this post


Link to post
Share on other sites

>The frequency of the fs9.exe unhandled exception (during>aircraft unload and/or simulator shutdown) error was reduced>by my pointer correction, but hadn't gone completely away>until I did as you had suggested, moving the GDI startup code>into PANEL_SERVICE_CONNECT_TO_WINDOW and the shutdown code>into PANEL_SERVICE_DISCONNECT. >>We'll see if that holds true, but for now, I've started and>shutdown 50-60 times, and no crash.Interesting, to say the least. I've never experienced any problems whatever with starting GDI+ from here:void FSAPI DF_gaugecall(PGAUGEHDR pgauge, int service_id, UINT32 extra_data){ if(!init_digitrack) { GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); init_digitrack = true; }nor have I experienced any problems with using PRE_KILL for shutdown...Perhaps I've just been lucky... ;)But, I'll certainly bow to Hans on this matter, and amend my wayward ways! *:-*

Share this post


Link to post
Share on other sites

Bill,There's absolutely no problem in using POST_INSTALL/PRE_KILL. The only disadvantage is that it's called every time the FS windows is resized. If you use CONNECT/DISCONNECT this happens only once when the gauge is loaded. It's nothing more than a matter of efficiency and not really important.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this