Solved

GDI Resource Leak

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 clear a vector as well as how to detect empty vectors in C++.

728 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