Deleting Windows Handle/Objects

Started by
6 comments, last by harmless 20 years, 5 months ago
Hi everyone, i have this simple question on handling objects in windows API. Here's my code: HBITMAP holdbmp, hNewBmp; HDC hdc; HWND hwnd; hdc = GetDC(hwnd); hNewBmp = CreateCompatibleBitmap(hdc, 100,100); hOldBmp = SelectObject(hdc,hNewBmp); . . . SelectObject(hdc, hOldBmp); DeleteObject(hNewBmp); ReleaseDC(hdc); I didn't delete hOldBmp object since i didn't create it anyway. Now here's my question: HDC hdc,copydc; HWND hwnd; hdc = GetDC(hwnd); copydc = hdc; . . . ReleaseDC(hdc); ReleaseDC(copydc); // <--- do i still have to do this? Any helpful comment would be greatly appreciated. Thanks in advance. [edited by - harmless on October 22, 2003 1:30:22 AM] [edited by - harmless on October 22, 2003 1:31:35 AM]
Advertisement
No. You only need one ReleaseDC per GetDC.
.
quote:Original post by Mastaba
No. You only need one ReleaseDC per GetDC.


so if i ReleaseDC(hdc), it does the same thing as ReleaseDC(copydc)?
How about if i ReleaseDC(hdc), then still call ReleaseDC(copydc), i should expect error return from ReleaseDC(copydc) because its copy is already previously released am i correct? or am i confusing myself?


All structs in windows with an H infront of it is a pointer to a structure. So if you do something like this:

int* myInt = new int; // HDC hdc = GetDC(hWnd);
int* ptrInt = myInt; // HDC hdcCpy = hdc;

You only need to delete one of those pointers as they point to the same location. It''s the same with HDC, HWND, HFONT, etc...
Incorrect. The prefix "H" stands for "Handle"

A handle is nothing more than a unique 32-bit identifier for some system resource. While quite similar to pointers, they definitely ARE NOT POINTERS.

Think of them as an ID code. If you ReleaseDC()/CloseHandle() one, then any other handle floating around with the same bits as the one freed will no longer be valid.
daerid@gmail.com
*dang*
quote:Original post by daerid
Incorrect. The prefix "H" stands for "Handle"

A handle is nothing more than a unique 32-bit identifier for some system resource. While quite similar to pointers, they definitely ARE NOT POINTERS.

Think of them as an ID code. If you ReleaseDC()/CloseHandle() one, then any other handle floating around with the same bits as the one freed will no longer be valid.


OK thanks!. no it seems much more clear to me. if they were pointers to struct, it made me much more confused =)
Anyways thanks for help =)

Now, before anybody goes and gets technical on me, let me clarify something: When I say that HANDLE''s "ARE NOT POINTERS", I''m talking about intent and semantics of the concept, not what they are actually defined in code as.

Here''s the definition of HANDLE and DECLARE_HANDLE() (which is used to define all the other H* types) in windef.h:

typedef void *HANDLE;#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name


which, says that yes, technically, handles are pointers. However, they are not meant to be used as pointers, and do not have all pointer semantics.
daerid@gmail.com

This topic is closed to new replies.

Advertisement