Widget Destruction

Hi all.

Using 6.1.

What is the correct (and reliable) way to see if a widget still exists?
PtWidgetIsRealized does not help, as I want to access the widget whether it
is realized or not. At the moment, I am checking if (ABW_widgetname !=
NULL), but it would seem that this is not a reliable test. I would have
thought that PhAB-generated modules would reset the widgets’ global pointers
after a Done: or Cancel:, but it seems not.

A bit more explanation:

When the user presses a button, the application shows a result window, and
creates a lower-priority thread to do some lengthy calculations. When the
calculations are complete, the thread (PtEnter(0), PtLeave(0)) updates the
the result window with the results. However, if the user exits the result
window before the calculations are complete, the thread crashes the
application by trying to access the now-destroyed widgets of the result
window.

TIA,
Stuart Harding

Ahhh…

if (ABW_widgetname != NULL)
if (!(PtWidgetFlags(ABW_widgetname) & Pt_DESTROYED))
AccessWidgetSafely();

Thanks All,
Stuart Harding


“Stuart Harding” <stuart@intellidesign.com.au> wrote in message
news:b3k5o2$41p$1@inn.qnx.com

Hi all.

Using 6.1.

What is the correct (and reliable) way to see if a widget still exists?
PtWidgetIsRealized does not help, as I want to access the widget whether
it
is realized or not. At the moment, I am checking if (ABW_widgetname !=
NULL), but it would seem that this is not a reliable test. I would have
thought that PhAB-generated modules would reset the widgets’ global
pointers
after a Done: or Cancel:, but it seems not.

A bit more explanation:

When the user presses a button, the application shows a result window, and
creates a lower-priority thread to do some lengthy calculations. When the
calculations are complete, the thread (PtEnter(0), PtLeave(0)) updates the
the result window with the results. However, if the user exits the result
window before the calculations are complete, the thread crashes the
application by trying to access the now-destroyed widgets of the result
window.

TIA,
Stuart Harding



\

Stuart Harding <stuart@intellidesign.com.au> wrote:

What is the correct (and reliable) way to see if a widget still exists?

The only completely general way is to keep track of when it’s destroyed,
perhaps by attaching a DESTROYED callback. But see below.

PtWidgetIsRealized does not help, as I want to access the widget whether it
is realized or not. At the moment, I am checking if (ABW_widgetname !=
NULL), but it would seem that this is not a reliable test. I would have

This only works for the main widget of a dialog module (i.e. its
window).

thought that PhAB-generated modules would reset the widgets’ global pointers
after a Done: or Cancel:, but it seems not.

Only the top-level pointer, and only for dialog modules.

A bit more explanation:

When the user presses a button, the application shows a result window, and
creates a lower-priority thread to do some lengthy calculations. When the
calculations are complete, the thread (PtEnter(0), PtLeave(0)) updates the
the result window with the results. However, if the user exits the result
window before the calculations are complete, the thread crashes the
application by trying to access the now-destroyed widgets of the result
window.

If you make sure that the window is a dialog module and you check the
ABW pointer of the window rather than some other widget in the window,
this should work.


Wojtek Lerch QNX Software Systems Ltd.

Stuart Harding <stuart@intellidesign.com.au> wrote:

if (ABW_widgetname != NULL)
if (!(PtWidgetFlags(ABW_widgetname) & Pt_DESTROYED))
AccessWidgetSafely();

No, this only works until the widget’s dead body is freed, which
typically hapens soon after all its callbacks have returned. After the
widget has become free memory, there’s a good chance that some unrelated
malloc() call will grab it and give it to someone who may clear the bit
that used to be the Pt_DESTROYED flag in the old widget.

It’s even possible that a new, completely unrelated widget will be
created at the same address, fooling you into believing that the old
widget is still alive.

At least among widgets, reincarnation is a fact!