[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Freeing resource for custom caret's bitmap

Posted on 1998-09-15
3
Medium Priority
?
283 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 100 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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

872 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