Solved

GDI Resource Leak

Posted on 2001-08-09
2
580 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

770 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