CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Exclamation 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!

  2. #2
    Join Date
    Aug 2010
    Posts
    2

    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()

  3. #3
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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?
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured