Solved

GDI Resource Leak

Posted on 2001-08-09
2
578 Views
Last Modified: 2012-08-13
The following code causes a memory leak in my app

HBITMAP m_hLogoBMP = LoadBitmap( _Module.m_hInst, MAKEINTRESOURCE( IDB_GENDAC ) );
if( m_hLogoBMP == NULL )
     return false;
HDC m_hLogoDC  = CreateCompatibleDC( NULL );
SelectObject( m_hLogoDC, m_hLogoBMP );

BitBlt( GetDC(), 30, 30, 180, 142, m_hLogoDC, 0 , 0 , SRCAND );

DeleteDC( m_hLogoDC );
DeleteObject( m_hLogoBMP );

If I comment the BitBlt function out I have no resource leak.

Thanks
Daniel.
0
Comment
Question by:danielj040300
2 Comments
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
ID: 6367643
The reason is, that before delete some bitmap, you must
restore old bitmap, connected to m_hLogoDC.
And ReleaseDC after GetDC!
So , your code must be:
...
HBITMAP hOld = SelectObject( m_hLogoDC, m_hLogoBMP );
HDC hdc = GetDC();
BitBlt( hdc, 30, 30, 180, 142, m_hLogoDC, 0 , 0 , SRCAND );
SelectObject( m_hLogoDC, hOld);
ReleaseDC(hdc);
...



0
 
LVL 1

Author Comment

by:danielj040300
ID: 6367722
I only had to call the ReleaseDC(hdc); function.
No more Resource leak!!
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

707 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

14 Experts available now in Live!

Get 1:1 Help Now