Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I got GDI Resources Bloat

Posted on 2002-07-25
7
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 7179316
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
ID: 7180413
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
ID: 7181250
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:fl0yd
ID: 7181283
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
ID: 7182105
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 300 total points
ID: 7182169
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
ID: 7454244
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

636 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