?
Solved

Asset when exiting OnPaint event

Posted on 2003-03-02
3
Medium Priority
?
927 Views
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.

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

void NotesDlg::DrawSampleText()
{
     HFONT     hFont;

     hFont = CreateFont( 30,     0, 0, 0, FW_NORMAL,
               FALSE, FALSE, FALSE, DEFAULT_CHARSET,
               OUT_STROKE_PRECIS, CLIP_DEFAULT_PRECIS,
               DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE,
               FontName);

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

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

     m_sampleText.InvalidateRect(NULL, true);
     m_sampleText.UpdateWindow();

     cdc->SetTextColor(GetNoteColourVal());
     cdc->SelectObject(hFont);
     cdc->DrawText(FontName, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
     m_sampleText.ReleaseDC(cdc);
}
</PRE>

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.

<PRE>
void NotesDlg::OnPaint()
{
#ifdef _DEBUG
     int nPrevMode = _CrtSetReportMode(_CRT_ASSERT,0);
#endif
     if (PrydaTest)
     {
          CPaintDC dc(this);
          DrawSampleText();
     }
#ifdef _DEBUG
     _CrtSetReportMode(_CRT_ASSERT,nPrevMode);
#endif
}
</PRE>

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.
0
Comment
Question by:supunr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

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

m_sampleText.ReleaseDC(cdc);

but ReleaseDC needs a pointer:

m_sampleText.ReleaseDC(&cdc);
0
 
LVL 11

Accepted Solution

by:
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.
0
 
LVL 11

Author Comment

by:supunr
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
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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