Link to home
Start Free TrialLog in
Avatar of FunkyMeister
FunkyMeister

asked on

Resources burn up!

I'm working with:

CreateCompatibleDC, CreateCompatibleBitmap, DeleteObject and DeleteDC.

I have a re-enterable routine that will size the Compatible Bitmap as needed.

The problem is:

if Bitmaphandle<>0 then DeleteObject Bitmaphandle ' Previous bitmap
CreateCompatibleBitmap .... 'build bitmap and store in Bitmaphandle

Doing this, repeatively (when size changes happen), causes exhaustive resource burning (in the high percentage ranges, like almost 10% each time).

There is only one hDC being used, it's not deleted, each DeleteObject returns 1 (success), so I'm at a large loss as to why this is happening right now, because if I delete the object, the memory should be freed up, but it isn't (until the program exits, which is bad).  Is there a way I can get the system to do this FREE up when I need it to?

I must be over looking an API function for GC.  It just makes no sense that I need to ask for it.  Getting past this, will allow me to finish a calendar stamping function class (that lets you stamp a calendar onto a bitmap).

Any stabbers?
Avatar of GivenRandy
GivenRandy

Make sure you delete both the Pen and Brush after using them.  If you defined Pen and Brush, use:

Result = DeleteObject(Pen)
Result = DeleteObject(Brush)
Avatar of FunkyMeister

ASKER

I pull up all the brushes (no pens) I need at the start.  Fonts are a problem, I keep rehashing the same font (just for size), there must be a better way to do that.  I also remove and rebuild the bitmap (because it's size is not right for the current needs).

I create the compatible hdc, brushes (2) and borrow the desktop hdc.

At the end of the program, I release the desktop dc I borrowed, the two brushes are deleted as is the compatible hdc.  (The bitmap is done as well, if it still exists.)

At a complete loss.  Only thing I can think of is the font is causing it.  I've checked everything else.  (I think I'm going to make it only make 1 font change and see if the leak vanishes.)  It causes System and GDI to drop like a rock.  I've seen it go from 80%+ to less than 30% in a split second.

Thankfully I watch them and cancel the operation of the program (so it doesn't cause a crash).
Compuware has DevPartner Studio which does a great job of detecting things like this:

http://www.compuware.com/products/devpartner/studio/

They used to have a trial period, which you might check.
Actually, it looks like the problem is the bitmap not being deteted by the DeleteObject.  It's returning a 1 as being true, but it's incorrect, as every time the bitmap is deleted and recreated, the previous map appears to remain in memory.  I tested the font change, it's not the issue, as soon as the map gets larger, boom.  Resources drop like a rock (but they shouldn't).

I'll check that dev partner and see what it has to offer, but do you or anyone have a "sane" DeleteObject example that actually DOES delete a Compatible Bitmap?
ASKER CERTIFIED SOLUTION
Avatar of GivenRandy
GivenRandy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I gave it to you because I already found the answer (prior), but you found better resources for me to peek at, so I thought it'd make you happy.  :)

I solved all the problems earlier by realizing something in the docs for deleteobject.  If it's IN the Device Context, it can't be removed (makes sense), but what they don't tell you is it returns an ok response if it didn't delete it.  :/  SDK fooleries.  :(