?
Solved

GDI Resource Leak

Posted on 2001-08-09
2
Medium Priority
?
586 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

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