Solved

Loading bitmaps in a VC++5.0 dialog box

Posted on 1998-09-17
5
202 Views
Last Modified: 2010-05-18
I'm writting a game in a dialog box that is constantly displaying bitmaps with the code:
void CMyDlg::DisplayMyPic()
{
      CClientDC dc(this);
      //creat a memory DC
      HDC hMemDC = ::CreateCompatibleDC(NULL);
      HBITMAP hbitmap;
//draw mybitmap
      hbitmap =
      ::LoadBitmap(::AfxGetInstanceHandle(),
            MAKEINTRESOURCE(IDB_MYBITMAP));
      //creat a memory DC
      HDC hMemDC = ::CreateCompatibleDC(NULL);
      //select the bitmap in the memory dc.
      SelectObject(hMemDC,hbitmap);
      //Copy the memory dc into the screen dc
      BitBlt(dc.m_hDC,x,y,16,16,hMemDC,0,0,SRCCOPY);
      DeleteObject(hbitmap);
}

This will display the bitmap, but it seems that the memory is getting used up, causing the program to lock up after a minute of running.  Can anyone tell me if there is a way to load the bitmaps up at the begining of the program and then select them to display, or delete the object so my program won't lock up?
0
Comment
Question by:SileNcer
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
First of all the real problem is that you are creating DCs and bitmaps and not freeing them.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
In the above code, you use CreateCompatibleDC().  This creates a DC object in the windows GDI heap and returns a pointer (handle) to it.  If you don't free this DC with DeleteDC() it will continue to use up space in the GDI heap.  If you create a new one each time this procedure is called, then you will eventually fill the GDI heap.

You should either call DeleteDC at the end of this procedure to delete the DC (note you create 2 DCs for no apparent reason, you should only create 1 or you will need 2 handles so you can track both DCs.)  or you should create 1 DC at the start of the program and use it here and then delete it at the end of the program.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Another problem is that you do not select the original bitmap back into the DC.  Whenever you select something new into a DC (pen, brush, bitrmap)  You must select the original one back in before you return the DC to the OS (with DeleteDC() or similar function).  In this case, the windows returns a DC to you that specifies a memory bitmap.  It will want to delete that bitmap when it deletes the DC.  However, it will expect to find a handle to that bitmap in the DC when DeleteDC() is called.  Thus if you don't select the original bitmap back when deleting the DC, it will delete your bitmap and the other bitmap won't get deleted.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> Can anyone tell me if there is a way to load the
>> bitmaps up at the begining of the program and then
>> select them to display, or delete the object so my
>> program won't lock up

Yes, just create an array to hold the handles to all the bitmaps you will need.  At the program start, load all the bitmaps and store their handles in the array.  When drawing, obtain the desired handle from the array.  When you are done, remember to delete all the bitmaps specified in the array.
0
 

Author Comment

by:SileNcer
Comment Utility
Thanks a lot!  In my program I only created 1 DC, I just copied it wrong, sorry.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

11 Experts available now in Live!

Get 1:1 Help Now