Solved

Freeing resource for custom caret's bitmap

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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