Archived

This topic is now archived and is closed to further replies.

harmless

Deleting Windows Handle/Objects

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites