CreatePen() return null in timerproc() after running for about 100s
Thank you for your attention, I met this problem in my timerproc function.
I need to change the color of the pen according to a changing value, so I need to createpen in this timerproc. After running normally for about 100s, pen.createpen() begins to return 0. If I step over in the debug mode, it enters wincore.cpp when it step over the pDC->SelectObject() function.(Is it an exception?)
The GDI resource didn't increase when I ran the program even when it got stucked at about 100s(the timerproc receive a WM_TIMER message every 30ms), neither did the CPU or the Memory. The GetLastError() function returns 0 right after the createpen() function. And the program cannot step over pen.SelectObject(), if there is a breakpoint before the SelectObject() function, it goes there when I try to step over SelectObject(); if there isn't, it enters the wincore.cpp as I said above.
I paste my code below,
CDlg::TimerProc(....)
{
...
pDC=pThis->picturecontrl.GetDC();
CPen pen;//
pen.CreatePen(...) //CreatePenIndirect() returns 0 here too
old_pen=pDC->SelectObject(&pen);//cannot step over here
...
pDC->SelectObject(old_pen);
pen.DeleteObject();
pThis->ReleaseDC();
...
}
Looking forward to your help, sincerely!
Re: CreatePen() return null in timerproc() after running for about 100s
Now, I found that after running for about 100s, the GetDC() returns 0 too;
p.s. the last line is pThis->ReleaseDC(pDC) instead of pThis->ReleaseDC()
Re: CreatePen() return null in timerproc() after running for about 100s
Hi,
I'm not sure what picturecontrl is, but should you not free the DC like this:
Code:
pThis->picturecontrl.ReleaseDC(pDC)
Re: CreatePen() return null in timerproc() after running for about 100s
Don't use GetDC() at all!
Use CClientDC class. It makes all Get/Release for you so you shouldn't care!
Besides, what is the purpose of TimerProc? Why not just handle WM_TIMER message in the CDlg class? :confused: