Solved

I got GDI Resources Bloat

Posted on 2002-07-25
7
317 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 100 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 186
viewing source code from eclipse 13 107
canBalance challenge 34 86
wordappend challenge 8 179
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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