Solved

Loading bitmaps in a VC++5.0 dialog box

Posted on 1998-09-17
5
203 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

919 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

18 Experts available now in Live!

Get 1:1 Help Now