|
-
Jul 28th, 2001, 02:32 PM
#1
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
-
Jul 28th, 2001, 06:06 PM
#2
-
Aug 20th, 2001, 03:30 AM
#3
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
-
Aug 20th, 2001, 02:27 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|