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!