Function to check if CWnd still created

1024 views
Skip to first unread message

maka3

unread,
Jul 20, 2004, 5:29:07 AM7/20/04
to
Hi,
I'm looking fur a function to check if a CWnd pointer is still valid,
what means that he window has not been closed.
Thanks,
Markus

Josema

unread,
Jul 20, 2004, 7:10:51 AM7/20/04
to
Hi,

you can try ::IsWindow (HWND) or CWnd::GetSafeHwnd ()


"maka3" <ma...@punjabi.net> escribió en el mensaje
news:9f73cf3f.0407...@posting.google.com...

Ajay Kalra

unread,
Jul 20, 2004, 10:23:22 PM7/20/04
to
Try ASSERT_VALID if you want to do it in debug mode. Other suggestions
should work as well.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"maka3" <ma...@punjabi.net> wrote in message
news:9f73cf3f.0407...@posting.google.com...

Joseph M. Newcomer

unread,
Jul 20, 2004, 10:19:33 PM7/20/04
to
That is very difficult. You are faced with two questions here: (a) is the CWnd pointer
valid (very hard) and (b) is the window referenced by the CWnd object valid (very easy).

If the window is closed, under normal conditions the accompanying CWnd * object has also
been "destroyed", that is, the storage referenced by the CWnd * has been released to the
heap, and may either now represent free space, or have been reallocated for another
purpose. Therefore, the pointer is ALWAYS "valid", in that it points to valid storage. The
storage, however, may not be meaningful (there are rare cases in which the storage might
actually be freed back to the OS, but that doesn't happen often enough to matter. You
almost always end up with a valid pointer to meaningless storage).

Therefore, it is best to code in ways that do not rely on knowing a window pointer is
valid.

The most common context comes up in the case of having to deal with threads, where a
thread wishes to PostMessage to a CWnd *. In this case, the answer is simple: the CWnd *
that is receiving the message is responsible for making sure the thread is shut down
before it allows itself to be closed. If this is your problem, post a followup and I'll go
into the more extensive discussion this requires.

Otherwise, you should consider it a coding error that you can be holding onto a CWnd * to
a window that could be destroyed out from under you, and you will have to change your
program. There is no way to tell if a CWnd * pointer is valid.

OTOH, if you have a CWnd * you know CAN be valid, but the window can go away...a rare
condition, given the nature of MFC, then you can use ::IsWindow on the m_hWnd member to
see if it is still a window. There are other tests, for example, CWnd::GetSafeHwnd() can
tell you during startup if a control has been initialized.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

maka3

unread,
Jul 21, 2004, 7:21:21 AM7/21/04
to
Joseph M. Newcomer <newc...@flounder.com> wrote in message news:<e7krf01ef02l45akh...@4ax.com>...

> Otherwise, you should consider it a coding error that you can be holding onto a CWnd * to
> a window that could be destroyed out from under you, and you will have to change your
> program. There is no way to tell if a CWnd * pointer is valid.

Hi Joseph,
Thanks for your detailed answer. I create a new window with
pMainFrame->CreateNewChild() and save a pointer to it. In the render
loop I draw to it with DirectDraw.
I could get in the WindowProc and draw in WM_PAINT. But how can I jump
in the Proc
with only having the pointer?
I'm very new to MFC and started programming with C#, where it is a
common way to call for example "Form.Created".

Joseph M. Newcomer

unread,
Jul 21, 2004, 6:56:15 PM7/21/04
to
I've not used DirectAnything, so I can't answer that specific question. But if you create
the child window, you know when you are going to destroy it; all you have to do is make
sure you don't try to use it before it is created (e.g., set the pointer to NULL) and
don't try to use it after it is destroyed (which a correct program should be able to deal
with).
joe

Joseph M. Newcomer [MVP]

Reply all
Reply to author
Forward
0 new messages