Click to See Complete Forum and Search --> : Safety of ::IsWindow
souldog
May 28th, 2004, 03:48 PM
I have question about using ::IsWindow
Suppose I have a window which creates a worker thread.
inside the worker thread messages are posted to the window
like this
if(::IsWindow(hwnd))
::PostMessage(hwnd, .....);
where the HWND is passed into the thread.
Now the question is, if this thread outlives the window, will this
give me problems?
kirants
May 28th, 2004, 04:11 PM
Hm.. that is what IsWindow does, right ? If the parameter passed is not a window , it should return error.
Was that what you were asking ?
souldog
May 28th, 2004, 04:36 PM
I guess I am worried that the handle may get recycled and I will
have a bogus value in my thread.
I guess I am asking: Does this happen for an HWND to a window
I create in the process?
kirants
May 28th, 2004, 04:38 PM
I guess you were driving at that. I think it is possible that the hWd value could be recycled. I'm not sure of the internals :(
NoHero
May 29th, 2004, 07:49 AM
I thought that windows does not recycle ANY handles ... (so I always call DestroyWindow ...) ... :( But I do not know excactly ...
Marc G
May 29th, 2004, 09:34 AM
Recycling handles doesn't mean that you don't have to call DestroyWindow.
Once you have called DestroyWindow, the handle is no longer associated with your window, and Windows might use that same handle for a new window that is created in the future.
Bond
June 1st, 2004, 11:37 AM
If you're really concerned about recycling (I wouldn't be), you can always check to see that the window was created by YOUR process...
DWORD dwProcessId;
::GetWindowThreadProcessId(hwnd, &dwProcessId);
if (dwProcessId == GetCurrentProcessId() && ::IsWindow(hwnd))
{
}
souldog
June 2nd, 2004, 01:27 AM
Thanks for all the responses. I know the chances of running into
a problem with recycling are very small. I just have a problem with
worrying about details (hmmm... is that really a problem?)
There seems to be no full proof way to protect against this problem
However, it is so unlikely to cause problems that I guess most
people just ignore it.
I know for sure that I create the window. This is really just a
shutdown issue, the threads live in DLLs and I want to keep the
responsibility for the lifetime of the threads contained in the DLL
modules. The client executable where the window lives just needs to notify the thread some how that it is closing.
I could use a thread message to achieve this, but I choose to
use a interlocked flag. The thread just check the variable through interlocked compare exchange and does not post a message if the flag has been set.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.