Results 1 to 4 of 4

Thread: Crash after 100 runs.

  1. #1
    Aragorn
    Guest

    Exclamation Crash after 100 runs.

    Hi.
    I've got a drawing function that will crash after 100 runs. I am getting a null pointer out of BeginPaint everytime, so i use GetDC and it works (only 100 times, sorry Technocrat ).
    It's like there is some sort of overlaod or overflow in the DC stack or so: the NULL return of BeginPaint indicates that "there is no DC available" according to MSDN, but then it is forced to give me a DC when i call GetDC. So a new DC gets created everytime i run the function, and strangely enough, it doesn't get released...

    The cause might be the way i register the class. This is how i do it:

    Code:
       EditorClass.style = CS_OWNDC;           //   Here i tried everything!
       EditorClass.cbSize = sizeof(WNDCLASSEX);
       EditorClass.lpfnWndProc = EditProc;
       EditorClass.cbClsExtra = 0;
       EditorClass.cbWndExtra = 0;
       EditorClass.hInstance = g_hEdInst;
       EditorClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
       EditorClass.hCursor = LoadCursor(NULL, IDC_ARROW);
       EditorClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
       EditorClass.lpszMenuName = NULL;
       EditorClass.lpszClassName = g_szEditClassName;
       EditorClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    and the function is

    Code:
    void network::DrawNetwork(HWND hwnd) {
       HDC hDispl;
       LPPAINTSTRUCT lpPaint;
       RECT update;
       unsigned int i;
       char str[3];
       RECT rect;
       static unsigned int counter;
    
       if (GetUpdateRect(hwnd, NULL, 1)) {
          hDispl = BeginPaint(hwnd, lpPaint);
          hDispl = GetDC(hwnd);
    
    // pass counter
             itoa(counter++, str, 10);
             rect.left=0;
             rect.right=0;
             rect.top=SIZE*3;
             rect.bottom=SIZE*3;
    
             DrawText(hDispl, str, strlen(str), &rect, DT_NOCLIP);
    //
    
    //   function body.
    
          if (ReleaseDC(hwnd, hDispl)!=1) MessageBox(0, "Warning", "Error: Display Context not released!", MB_OK);
          EndPaint(hwnd, lpPaint);
       }
    }
    I am sure that the cause of the error is The BeginPaint & GetDC function since when i comment it out, the function doesn't crash, though i don't get the view...
    Also, if i comment out the BeginPaint but not the GetDC the window keeps on repainting all the time until the counter reaches 100, of course, and then crashes.

    I found some article on MSDN that explains somethings about the way DC's are handled and how does this depend on how you register the window class, specially the class style, but it didn't seem to solve the problem. I've tried all styles that seemed reasonable.

    Would it be good to use a static HDC such that i don't have to call BeginPaint or GetDC everytime? That doesn't sound right to me.

    Does anybody have a clue, an intuition, a guess?

    I'M DESPERATE!

    Thank you in advance

  2. #2
    Aragorn
    Guest

    problem solved

    Sorry guys, but i already solved the problem
    The
    Code:
    LPPAINTSTRUCT lpPaint;
    had to be changed to
    Code:
    PAINTSTRUCT Paint;
    and consequently i had to change lpPaint to &Paint. This way BeginPaint returns the right HDC and the "stack" (as i call it already) problem is solved.
    Anyway, i thought that both ways were effectively the same, but it seems that BeginPaint doesn't accept the way i was doing it.
    Do you know what is different in both ways?

    Anyway, this has stormed me for days. I'm glad that i finally solved it.

    bye

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Had that problem once too, but with a TEXTMETRICS structure.
    The problem is that LPPAINTSTRUCT is the same as PAINTSTRUCT*. a noinitialized pointer pointing to an invalid memoory block. This makes BeginPaint (or in my case GetTextMetrics) return NULL/FALSE, because there is nowhere to write to.
    CornedBee

  4. #4
    Aragorn
    Guest

    Thumbs up

    thanks CornedBee! This is about what i expected, but my knowledge lacks some basics, so I could'nt figure it out for sure.
    Well, you always learn something new every day.
    thank you

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