Solved

The correct way to DeletObject

Posted on 2004-04-27
6
303 Views
Last Modified: 2008-02-01
Hi,

I've read a ton of different info. on how to clean up bitmap objects but alot of the information is different.   Can someone explain what the correct method is for cleaning up a GDI object such as a bitmap. I.E.

HDC pDC = ::GetWindowDC(hWnd);
hBitmapDC = ::CreateCompatibleDC(pDC);
HBITMAP       hBitmap = ::LoadBitmap(hInst,(LPCTSTR)nResID);

::SelectObject(hBitmapDC,hBitmap );
::BitBlt(pDC,0,0,10,10,hBitmapDC,0,0,SRCCOPY);
                        
...How do I clean up properly?
0
Comment
Question by:alsmorris
[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
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:MattAA
ID: 10930410
You clean up any bitmap loaded with LoadBitmap by calling DeleteObject on its handle.  If this was a pen or a brush, you would have to unselect it from the device context before you could delete it, but there shouldn't be any problems with bitmaps.  So, to cleanup after that code, you would simply put:

::DeleteObject(hBitmap);

You should also delete your memory device context (hBitmapDC) with ::DeleteDC when you are done with it.
0
 

Author Comment

by:alsmorris
ID: 10930494
MattAA,

I have tried this but I get resource errors... and finally the window goes black.  I am finding out that what works on 2000/XP does not work on 98.  For example what you said ::DeleteObject(hBitmap); will not give me a problem on 2000/xp but on 98 I get a black window and boundchecker tells me that the object is still selected in the DC.

Thanks :)
0
 
LVL 8

Accepted Solution

by:
_corey_ earned 100 total points
ID: 10931506
You need to repeat your SelectObject call first to make sure.

Select all objects out of the DC, then destroy the DC and the objects.
0
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!

 

Author Comment

by:alsmorris
ID: 10931570
ok ... I think I figured it out...But I'm not sure this is the best way to do it...

HDC pDC = ::GetWindowDC(hWnd);
hBitmapDC = ::CreateCompatibleDC(pDC);
HBITMAP      hBitmap = ::LoadBitmap(hInst,(LPCTSTR)nResID);

HBITMAP hOld = ::SelectObject(hBitmapDC,hBitmap );

::BitBlt(pDC,0,0,10,10,hBitmapDC,0,0,SRCCOPY);

::SelectObject(hBitmapDC,hOld);
::DeleteObject(hBitmap);
::DeleteDC(hBitmapDC);




0
 
LVL 8

Expert Comment

by:_corey_
ID: 10931600
That's about how DC/Object stuff goes.  You really need to keep it nicely cleared up for 98/etc because, if I'm not mistaken after this long, there are limited DC and GDI handles.
0
 
LVL 8

Expert Comment

by:_corey_
ID: 10931605
Actually, just repeating the SelectObject(hBitmapDC, hBitmap) call should restore by itself.
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

726 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