Solved

Freeing resource for custom caret's bitmap

Posted on 1998-09-15
3
280 Views
Last Modified: 2013-11-20
I'm using the code below to display a slanted caret in a custom edit window.   Although it displays fine, if I watch my resource meter, it appears as though system/gdi resources are not being freed properly.   I believe the resource for the bitmap within the caret is not released properly.  Any suggestions?

      if ( lf.lfItalic ) {
        int hh = lf.lfHeight ;
        HDC hDCwin = ::GetDC( hWnd ) ;
        HDC hDC = CreateCompatibleDC( hDCwin ) ;
        int ww = lf.lfWidth ;

        caretBitmap = ::CreateCompatibleBitmap( hDC, ww+1, hh ) ;
        prevBitmap = (HBITMAP) ::SelectObject( hDC, caretBitmap ) ;

        ::PatBlt( hDC, 0, 0, ww+1, hh, WHITENESS);
        ::MoveToEx( hDC, 0, hh, NULL ) ;
        ::LineTo( hDC, ww, 0 ) ;
        ::MoveToEx( hDC, 1, hh, NULL );
        ::LineTo( hDC, ww+1, 0 );
        ::PatBlt( hDC, 0, 0, ww+1, hh, PATINVERT );
        ::CreateCaret( hWnd, caretBitmap,caretWidth,caretHeight );

        ::DeleteObject( (HGDIOBJ) ::SelectObject( hDC, prevBitmap ) );
        ::DeleteDC( hDC ) ;
        ::ReleaseDC( hWnd, hDCwin ) ;
      }
0
Comment
Question by:JohnWeidner
[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
3 Comments
 
LVL 2

Accepted Solution

by:
jstolan earned 50 total points
ID: 1322165
Each call to CreateCaret must be matched to a call to DestroyCaret.  If you're done with it at the point in the code you're showing, then you can destroy it there.  If you need it to persist after this function terminates, then you need to move the CreateCaret function (perhaps to the point where the window is created), and place a matching DestroyCaret function at the point where the window is destroyed.
0
 
LVL 2

Expert Comment

by:jstolan
ID: 1322166
By the way, you'll still have to free the bitmap memory separately since DestoryCaret does not free this memory.
0
 

Author Comment

by:JohnWeidner
ID: 1322167
Yes, I was calling DestroyCaret() in another section of code.   But I was relying on the line    

     ::DeleteObject( (HGDIOBJ) ::SelectObject( hDC, prevBitmap ) );  

which I called right after creating the bitmap (see code from question) to free up the bitmap's memory.    However, eventhough the DeleteObject call was returning success, it must not have been working properly there.   I moved the DeleteObject call to the same section of code as the DestroyCaret and now the memory leak is gone.  

Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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