• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

Resource Leak?

Environment VC6.0, Windows 98.

I have a section of code that is throwing a ResourceException, but I can not
quite figure out why. The extract of code is called frequently to draw a
bitmap and a list of annotations. The drawing is repeated approximately once
every 100ms. After running for about 30 seconds the program crashes with an
unhandled resource exception.

Am I doing something bad?

Essentially the steps are:

1) grab an image from the capture card
2) convert the image to a device dependent bitmap.
3) create a memory DC
4) select the DDB into the memory context
5) add a list of annotations to the memory dc
6) bitblt the memory DC to the screen DC.

The resource exception occurs when constructing the CPen or CBrush for one
of the annotations. If I do not create a pen or brush the software does not

There are only 3 annotations in my list at present. The greater the number
of annotations the quicker the software crashes. This seems to suggest that
the system is running out of resource handles? But I believe I am returning
them to the systems at the end of the function call?

Any help would be much appreciated.
Thanks in advance, here is my code:

            CClientDC dc(this);

            // Get a Device Dependent Bitmap from the DIB.
            HBITMAP handle = GetDDB(bitmap_info, grabbedframe.framebits);

            CDC mem_dc;
            CBitmap* bitmap = CBitmap::FromHandle(handle);

            // select the grabbed frame in to a memory device context.
            CBitmap* old_bitmap = mem_dc.SelectObject(bitmap);

            for (int index = 0; index < annotation_list.GetSize(); index++)
                  // select the correct pen and brush for drawing the selected
                  // annotation.
                  CPen pen(annotation_list[index]->pen_style,

                  CBrush brush(annotation_list[index]->brush_colour);
                  if (annotation_list[index]->use_null_brush)

                  // draw the current annotation.

                        case AZANNOTATION::RECTANGLE:

                        case AZANNOTATION::ELLIPSE:

            // draw the bitmap to the screen.


            // remove any objects used in the memory dc.
            // I have also tried storing the last item returned from SelectObject
here. But I believe this does the same thing.

            // free up the bitmap handle.
  • 2
1 Solution
You create your pen and brush in the for loop, and select a null brush and pen outside of that loop. Not very elegant.

Save the return value from selectobject and re-select it at the end of the loop. Thus your WIN32 graphical objects handles will be freed correctly.
Here's an example:

CPen* pOldPen;
CDC dc;

for(int i=0; i<1000;i++) {
  CPen  pen(PS_SOLID, 1, RGB(0,0,0) );
  pOldPen = (CPen*)dc.SelectObject(&pen);
  // Draw something

For eevry selectobject of a pen you must re-select the old one before that pen gets destroyed. Since your pen is created on the stack you must do it before the end of the loop.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now