Solved

I got GDI Resources Bloat

Posted on 2002-07-25
7
314 Views
Last Modified: 2013-11-20
Hi all,
I'm in the final throes of finishing my program.  A friend told me that I should check for resource leaks by using the Task Manager in Windows 2000 and looking at the "GDI Objects" numbers.  They should increase and then decrease back to the same number as I open documents and then close them.  If I just open and close them, the numbers do go back to the same number.  But if I hit the "Open" button or change a font or do any number of things, the numbers don't go back to the previous number.  In fact, as I open and close documents and make changes the numbers keep getting higher.  I tried plugging all the holes by using DeleteObject in the destructors for any CFont that are held by a class.  I've released all my DCs.  I think I got them all.  But the numbers still increase (in fact, and this may be my imagination, it seems to be getting worse).  

My question is: am I making myself nuts for no reason?  Perhaps these numbers in the GDI Objects column of Task Manager don't really mean I'm leaking resources.  I know it's kind of general question but do increasing numbers mean a resource leak and what can anyone think of to try to fix it?  What are the common reasons for resource leaks besides not releasing any GetDCs or deleting font objects.  BTW, I thought CFont would automatically delete any GDI objects when it goes out of scope - ie when the class dies when I close the document.  Any help is greatly appreciated by my psychiatrist.
0
Comment
Question by:jjjkkklll
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
Yip, you're right on almost all of those points. All CGdiObject-derived objects do clean up after they go out of scope, i.e. the destructor is invoked. The "GDI Object" count in the task manager does still work correct - the secret is that windows manages those resources itself. It can decide on whether a GDI object is to be deleted or will continue to remain in memory after it is no longer needed.
A typical sign of a GDI resource leak is a CONSTANTLY rising number of GDI objects, since it is most likely to be somewhere in the OnDraw-function which is called frequently during execution. A higher number of GDI objects on program termination is not necessarily a sign of a resource leak - as long as it is moderate. While I prefer to have clean code, i.e. resources being released when they are no longer being used, it is a lot less dramatic on winnt systems than on win9x systems for two reasons: 1.) nt has support for an unlimited number of resources. 2.) when a process terminates resources are freed automatically. Don't get me wrong, I'm not advocating dirty programming - I'm merely putting it into perspective.
BTW, being a coder it still is of great help having a good psychiatrist.
0
 

Author Comment

by:jjjkkklll
Comment Utility
Thanks for the response, fl0yd.  So, if I get your drift, as long as I put in a good faith effort at cleaning up all my resources, I'll be okay.  The numbers don't rise constantly, but they rise consistently.  I have BoundsChecker and it's not showing any resource leaks.  Of course, it doesn't check for leaks until after the program terminates.  I was worried about the resources accumulating while the program is running.  (I don't know if that came through in my question.)  I take a tally of GDI objects with no "documents" open, open one, do some stuff, close it, and the GDI resources went up.  It seems to do it most consistently when I change the font in one of the Rich Edit controls.  But, I swear, I'm deleting all member CFont objects in the destructor, releasing all my DCs, etc.  I'm guessing this is just a Windows thing.  Like you said, it holds onto resources temporarily even after you explicitly let them go. I'm less worried about this after your comment.  Still, the shrink's on stand by.
0
 

Author Comment

by:jjjkkklll
Comment Utility
I noticed that when I open a new document and change a font in one of the Rich Edit boxes (using CFontDialog to choose the font), then I close the document, the GDI Object count goes up one.  It goes up everytime I choose a font that I hadn't chosen before.  If I choose one I have chosen before in an earlier (closed) document, the count stays the same.  Sounds like Windows is keeping fonts around that it thinks it may need again during the operation of this program.  I'm suspecting this is a Windows thing as fl0yd has suggested.  Anyone have any different theories?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
Out of pure curiosity. Do you happen to have access to a win9x-system? I'd be happy to hear how resources are handled on a system with a limited number of resources - 64k, if I'm not mistaken, or is it 32k?
0
 

Author Comment

by:jjjkkklll
Comment Utility
I have a Win98 and a Win95 box.  It seems to work fine on Win98 (I haven't tried 95 yet - it's out in the garage somewhere).  Of course, Task Manager in 98 doesn't have the cool "GDI Objects" option like 2000 so it's hard to see exactly what's happening with GDI.  Like I said, I have BoundsChecker on 98 and it doesn't show any real problems with resources.  

I'm still thinking that when I use the CFontDialog control to select a new font, Windows holds onto that font for a while to see if I need it again later.  That's why the number goes up when I select a new font, but doesn't when I select one I previously selected.  Seems like if this was a resource leak, the GDI object count would go up even if I picked the same font again.  Think I'm right?  
0
 
LVL 8

Accepted Solution

by:
fl0yd earned 100 total points
Comment Utility
Too bad, the win9x task manager doesn't show the GDI object count...

On to the font issue: I agree, if you were choosing the same font over and over again and the GDI object count would go up every time this would point to a resource leak. If it goes up by one and doesn't change on subsequently choosing the same font again this is an indication to windows' resource management system keeping it in it's most-recently-used heap. If I'm not mistaken you can control the maximum number of GDI objects that are left alive upon being freed by an application somewhere in the registry. Maybe I can dig it up...
0
 

Author Comment

by:jjjkkklll
Comment Utility
I thought I closed this one out months ago.  I'm giving the points to Fl0yd because he's the only one who answered me.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now