Solved

Freeing resource for custom caret's bitmap

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
paragon account problem 9 96
xyBalance chalenge 58 88
Best tools for Backup and restore of Wordpress site 6 53
modThree challenge 4 64
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

15 Experts available now in Live!

Get 1:1 Help Now