Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading bitmaps in a VC++5.0 dialog box

Posted on 1998-09-17
5
Medium Priority
?
209 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 150 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

609 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