Solved

Freeing resource for custom caret's bitmap

Posted on 1998-09-15
3
277 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 41
mixString challenge 36 119
Doc'in system (example?) BA 7 105
fizzArray  challenge 1 81
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

11 Experts available now in Live!

Get 1:1 Help Now