Solved

Loading bitmaps in a VC++5.0 dialog box

Posted on 1998-09-17
5
207 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
[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
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1172932
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
ID: 1172933
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
ID: 1172934
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
ID: 1172935
>> 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
ID: 1172936
Thanks a lot!  In my program I only created 1 DC, I just copied it wrong, sorry.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

695 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