[try Beta version]
Not logged in

 
Memory leak question

Jan 5, 2011 at 2:40pm
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!
Jan 5, 2011 at 2:49pm
Do I also have to delete the oldBrush object here?

No.
Last edited on Jan 5, 2011 at 2:49pm
Jan 5, 2011 at 2:56pm
Thanks :)

Why not?
Jan 5, 2011 at 3:12pm

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)
Jan 6, 2011 at 12:43pm
That makes sense, thank you very much :)
Topic archived. No new replies allowed.