Memory leak question

Hi,

I have this code (yes, I know black actually is #000000):

1
2
3
4
5
6
7
	HBRUSH black = CreateSolidBrush(RGB(00,00,01));
	HBRUSH oldBrush = (HBRUSH)SelectObject(this->hDC, black);

	Rectangle(this->hDC, 0, 0, this->width, this->height);

	SelectObject(this->hDC, oldBrush);
	DeleteObject(black);


Do I also have to delete the oldBrush object here? It's exactly the same type as the black object, but the examples I have don't delete it.

So the last line would become:
DeleteObject(black);
DeleteObject(oldBrush);

Thanks!
Do I also have to delete the oldBrush object here?

No.
Last edited on
Thanks :)

Why not?

DeleteObject(handle_to_object) removes that resource associated with that handle from the system - this makes the associated handle invalid!

1. You should not delete a resource while it is selected into a Display Context.
2. you should leave a Display Context as you got it - (with the original brush, pen, bitmap and font)
That makes sense, thank you very much :)
Topic archived. No new replies allowed.