Debugging code

Hey,

My Windows program doesn't work like it should: it doesn't do any drawing (it's a simple drawing program), it hangs when it should open a dialog box and it hangs when you try to exit the program.

I've tried to find out what's wrong, but can't find anything. There are no error messages or any information the IDE gives, so I have NO clue where to find the bug.

I would like to try to find it on my own before asking for help here, but I have no clue how to start debugging this. Any tips for this?


A small second question about a DC. Assume you get a DC this way:
HDC hdc = object->getDC();

The method is simple:
1
2
3
4
HDC Class::getDC()
{
	return DC;
}


The DC is initialised in the constructor:
DC = CreateCompatibleDC(hWndDC);


So you get the DC from the object's method. Should you remove that DC ("hdc" in this case) too? If I think logically, I would say yes, but a tutorial doesn't do so.

Thanks
Bv202
Last edited on
DC = CreateCompatibleDC(hWndDC); // DC is a handle to the actual Display Context.

1
2
3
4
HDC Class::getDC()
{
	return DC; //You are only returning a (copy of the) handle to the DC not the DC itself.
}



The actual DC that was was created in the constructor, should be deleted int destructor
DeleteDC(DC); // DC is a handle to the actual Display contexty.


With regard to the bit about your code locking up - post the code (it may be something to do with your WM_PAINT routine)
Last edited on
Hey,

Thanks for the reply.

My program at least does something already! It draws the primitives, but not correct.

1
2
3
4
5
6
7
8
9
10
case WM_PAINT:
hdc = backbuffer->getDC();
		if(MouseDown) 
			shape->draw(hdc);

		for(vector<Shape*>::size_type i = 0; i < shapes.size(); i++)
			shapes[i]->draw(hdc);
		
		backbuffer->copy();
return 0;


I don't think you have enough information with this. Just tell me if you need something more :)

Thanks
Last edited on
Hey,

According to the tutorial, I should draw a white rectangle over the whole area (to clear the backbuffer, then draw the primitives to the backbuffer, then present the backbuffer to the window. This needs to be done as first clearing Windows, then presenting backbuffer = 2 draws = still flickering).

So, the updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
case WM_PAINT:
hdc = backbuffer->getDC();

oldBrush = (HBRUSH)SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Rectangle(hdc, 0, 0, 640, 480);

		if(MouseDown) 
			shape->draw(hdc);

		for(vector<Shape*>::size_type i = 0; i < shapes.size(); i++)
			shapes[i]->draw(hdc);
		
		backbuffer->copy();
SelectObject(hdc, oldBrush);
return 0; 


This works and the flickering effect is gone (that was what I was trying). The drawing works fine now (there is still a small problem which I'm trying to find out now), but is this logic? Isn't this a HUGE memory waste now?

- A new, white rectangle is drawn in the backbuffer ("clearing" it)
- The shapes are drawn in the backbuffer
- The backbuffer is presented

But, on the next paint message, a new rectangle is drawn to the backbuffer to clear it. But the old shapes aren't removed from it; the rectangle is only drawn OVER these primitives. So with every paint message, the shapes from the previous paint message are presented too.

Or am I seeing this wrong?

Other than this, the program still hangs when trying to exit it/opening a dialog box. For some reason, I can open the dialog with pressing ctrl when the program hangs.

Thanks

EDIT:
The program uses about 25% CPU, so something is still terrible wrong with it.
Last edited on
But, on the next paint message, a new rectangle is drawn to the backbuffer to clear it. But the old shapes aren't removed from it; the rectangle is only drawn OVER these primitives. So with every paint message, the shapes from the previous paint message are presented too.

Or am I seeing this wrong?


You are seeing it wrong.
The new rectangle just overwrites anything there (at least any shape that that lies underneath it).

If you paste the entire code, I'll try and run it (mind you. I'm on windows 7) to see
what this 'hanging' is all about.
Topic archived. No new replies allowed.