# win32 rendering glitch

## Recommended Posts

Hello. I have been writing a game using win32 as a rendering engine, but it seems that it bugs out on me. I am drawing a football field consisting of around 700 sprites, and on the 20th refresh the program crashes. The call stack reveals the following:

[frames below may be incorrect and/or missing, no symbols loaded for user32.dll]

I am not sure what relevant code to show, as the program is pretty big. The following is the drawing part of the game:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	int mouseX, mouseY;	switch(message)	{//the rendering part	case WM_PAINT:		gGame.tick();		ghdc = BeginPaint(hWnd, &gps);		gGraphics.drawFrame();		EndPaint(hWnd, &gps);		break;	case WM_DESTROY:		PostQuitMessage(0);		break;//the onclick parsing	case WM_LBUTTONDOWN:		mouseX=(short)LOWORD(lParam);		mouseY=(short)HIWORD(lParam);		gSelection.onClick(mouseX, mouseY);				InvalidateRect (hWnd, NULL, TRUE);		UpdateWindow (hWnd);		break;	default:		return DefWindowProc(hWnd, message, wParam, lParam);		break;	}	return 0;}//this is the BBGraphics::draw() functionsvoid BBGraphics::drawFrame(){	for(unsigned int i=0;i<sprites.size();i++)		if(sprites[i])			sprites[i]->draw();}void BBGraphics::draw(RECT &rct){	HBRUSH brush=CreateSolidBrush(color);	FillRect(ghdc, &rct, brush);}//those are the BBSprite::draw() functions//both BBSprite and BBText are considered as spritesvoid BBSprite::draw(){	gGraphics.color=color;	gGraphics.draw(rect);}//BBText::draw()void BBText::draw(){	SetTextColor(ghdc, color);	TextOut(ghdc, rect.left, rect.top, text, text.length());}

Please note that the refreshing is done only after a mouse click, thus after 20 clicks the program crashes. I am really at loss for what this bug can be. Am I doing something wrong or is it the .dll that's bugging out? Any help would be appreciated.

##### Share on other sites
I can't see anything obviously wrong there, though I'm not an expert on Win32.

Your best approach is to put breakpoints everywhere and find at exactly which line the program crashes.

I do notice you are creating a brush in draw() every time the function runs. You probably need to destroy the brush once finished with it, or store the brush and reuse it. If the draw() function is being called many times per frame it could be a serious memory leak issue. Again, I'm not an expert, it's just something that jumped out at me.

##### Share on other sites
You need to clean up your handles properly, as dangerdaveCS noted.

Also, one thing you may find really helpful for debugging is setting up a link to the Microsoft Symbol Server, as detailed here. That'll let you see what functions are crashing in the Windows core code, and how that code is referenced from your own; in that case it should have quickly revealed the handle leak.

##### Share on other sites
Yeah, it was the HBRUSH problem. I just brainless copied the code from somewhere, thinking that HBRUSH was some kind of structure, not a handle (should have seen the H in the beginning of the name). Thank you, both of you for the help.