How can I know if a widget exists ?

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

5 posts / 0 new
Last post
How can I know if a widget exists ?

Submitted by orank on Tue, 11/27/2001 - 17:22. Developers
Hi ,
if I use XtVaGetValues on A widget ID that existed but now does not exist my Application crashes. Is there a motif function that I can use to find if the widget ID I have still points to an existing widget. In other words, is there a motif function that checks if a widget exist ?

ICS_support

No.

Basically, if you create the widget, you need to keep track of whether it still exists.

But occasionally, because of the distributed nature of the interface, one part of the application destroys the widget without knowing where else the widget has been used.

In that case, use the XtNdestroyCallback resource such that when the widget is destroyed, other parts of your application can be called so as to remove references to the widget.

For example (not valid code)

somefunction()
{
globals.currentWidget = widget;
XtAddCallback(widget, destroyCallback, resetCurrent, NULL);
}

void resetCurrent(...) /* callback */
{
if /*assert*/ (closure==globals.currentWidget)
globals.currentWidget = NULL;
}

In addition, you can track down this problem; see
http//www.motifzone.net/tnt/#object_destroy for details.

orank

Hi , thanks for the advice ,

it is funny to know that Motif has no simple answer fot this problem.

what I did was to keep a flag in my application to notify me a Wdiget delete and then zero the widget pointer to NULL, this way I workaround the problem insted of trying to really solve it.

ICS_support

Yes, that`s basically what the code I showed does, although I saved the step of having a separate flag.

The problem arises because Widgets are actually pointers to the data structure that the X Intrinsics uses to store information about the widget, so the only invalid value is NULL -- and you can`t check a field, such as by calling XtWindow(widget), without dereferencing freed memory.

I once saw a solution which did this
- as each widget was explicitly created via XmCreateWhatever or XtCreateWidget, it was logged into a hash-table, and the destroyCallback was added to it (the same callback for every widget)
- in the destroy callback, the widget was removed from the hash-table
- whenever there was possibly a doubt about whether the widget still existed, its presence was checked in the hash-table, which was a very fast operation

You can do this code as a sanity-check i.e. as an assert()-like operation to ensure that your application is following the flow of widget creation and destruction.

orank

thank you , the Hash table Idea is great,
I will try to keep it in mind.

All The Best
oran