Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Asset when exiting OnPaint event

Posted on 2003-03-02
Medium Priority
Last Modified: 2013-11-20
I am trying to draw sample text in a Picture control in MFC Dialog.  Following is the code I am using in order to draw the text, which changes as I change the colour and the text of the font.  

Therefore I have written a separate function to draw the text (DrawSampleText()) and OnPaint event calls that function.

void NotesDlg::OnPaint()
     CPaintDC dc(this);
} // <- Assert at this point

void NotesDlg::DrawSampleText()
     HFONT     hFont;

     hFont = CreateFont( 30,     0, 0, 0, FW_NORMAL,

     CDC* cdc = m_sampleText.GetDC();
     RECT     rect;

     ::GetClientRect(m_sampleText.m_hWnd, &rect);

     m_sampleText.InvalidateRect(NULL, true);

     cdc->DrawText(FontName, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

When I run the DEBUG version of the program and open the NotesDlg few times, program comes up with the Assert Message:<BR>
     Debug Assertion Failed!
     File: winhand.cpp
     Line: 199

I have highlighted above in the code where the Assert fail.  It happens when OnPaint is trying to exit the function.  Assert fail in the RemoveHandle() function defined in the winhand.cpp file with

the code....<BR>
          if (m_nHandles == 2)
               ASSERT(ph[1] == h);     //<- This Assert fail (value of ph[1] = NULL, but h != NULL)

I have tried number of different things such as removing the "CPaintDC dc(this)" line from the OnPaint() event, this caused and infinite loop of OnPaint event and eventually crashed the program.  

The only solution I could come up with was to turn off assert messages as shown below in the code.

void NotesDlg::OnPaint()
#ifdef _DEBUG
     int nPrevMode = _CrtSetReportMode(_CRT_ASSERT,0);
     if (PrydaTest)
          CPaintDC dc(this);
#ifdef _DEBUG

This program occurs on Windows 98 machines with about 128MB or less memory.  I was not able to reproduce this in Win2K with 256+MB RAM.  Does anybody know why this could be happening or how I could

correct my code to fix this Assert fail.

Btw, I do not have many points and I have allocated all the points I have for this question.
Question by:supunr

Expert Comment

ID: 8057375
In the last line from DrawSampleText():


but ReleaseDC needs a pointer:

LVL 11

Accepted Solution

KurtVon earned 300 total points
ID: 8057517
To be honest, I don't have a clue why it is happening either.  It's obvious that the program thinks you have two PaintDCs, but not why it thinks that.

However, here's a possible solution.  Instead of creating a CPaintDC, call the base class CDialog::OnPaint().  You may want to call it after your own painting, though.  If it still happens your problem is really somewhere else, and you need to find where the DC handles are getting corrupted.

By the way, don't forget to select the original font into the DC in your function DrawSampleText.  I don't think that is the problem, but it's always good practice to destroy a DC with its original objects.
LVL 11

Author Comment

ID: 8061021
cmaryus, cdc is already a pointer.  Therefore I have to use it as m_sampleText.ReleaseDC(cdc);, but not the way you suggested.  I have tried the way you mensioned, but then I got the compiler error saying it can not convert CDC** to CDC*.

KurtVon, I will copy the original font back to the sample.  Thanks for that comment.  One thing I forgot to mension is that the same problem happen in not just one, but 3 different dialogs, where all of them has very similar code, that is, OnPaint event calls some other function to draw a sample line and another to draw a preview of a selected picture.  They all come up with the same assert error.  Again, I was not able to produce the error on Windows 98 (same machine) with 512MB RAM, but it happens when I put 128MB RAM in.  May be with 512 MB RAM, it takes long time for that to happen, I don't know.   I have tried your suggestion and it seems to have worked fine.   Thanks for a quick response.  I am giving you all the points for your valuable comments a working solutions.  Thanks once again

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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.
Suggested Courses

578 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