home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!lsl!snail
- From: snail@lsl.co.uk
- Newsgroups: comp.windows.x.motif
- Subject: Re: XmSelectionBoxGetChild crashes application
- Message-ID: <1993Jan25.173251.2979@lsl.co.uk>
- Date: 25 Jan 93 17:32:51 GMT
- References: <5935@esf.esf.de> <1993Jan22.164824@mccall.com>
- Organization: Laser-Scan Ltd., Cambridge
- Lines: 91
-
- In article <1993Jan22.164824@mccall.com>, tp@mccall.com (Terry Poot) writes:
- >
- > In article <5935@esf.esf.de>, klaus@tat2.esf.de (Klaus Wicovsky) writes:
- >>----------------------------------------------------------------------
- >>void fs_okCB(w, client_data, call_data)
- >>...
- >>...
- >> text=XmSelectionBoxGetChild (w, XmDIALOG_TEXT);
- >> filename=XmTextGetSelection(text);
- >>...
- >> list = XmSelectionBoxGetChild (selected, XmDIALOG_LIST_LABEL);
- >>----------------------------------------------------------------------
- >>
- >>Whenever this callback procedure is carried out, my application crashes at
- >>the last line. "w" would be my original widget for which I define this
- >>callback
- >>and "selected" would be my new widget, which would like to modify.
- >>
- >>Although "selected" is globally defined, the last statement crashes my
- >>application. Any idea why this could happen ???
-
- You don't need global variables. Globals are poor engineering. See below.
-
- > Happened to me last week. If you check, I think you'll find that w is not the
- > widget id of the selection box, but of the OK button. XmSelectionBoxGetChild
- > appears not to check for the widget id actually being a selection box, so it
- > crashes with a memory access error. I don't know if XtParent would reliably get
- > you the widget id of the selection box, it depends on how Motif bundles all that
- > stuff together. You need to remember the widget id of the selection box by some
- > other method so it will be accessible to your callback.
-
- The last sentence....
-
- client_data should be used for this.
- For example:-
-
- a) Data structures
-
- struct my_data {
- MY_DATA_PTR self;
-
- Widget selected_w;
-
- other data....
- };
-
- typedef struct my_data *MY_DATA_PTR;
-
- b) Allocate some memory the size of a structure you have defined.
-
- MY_DATA_PTR mdp;
-
- mdp = (MY_DATA_PTR)malloc(sizeof(struct my_data));
- if (mdp == NULL)
- /* oh shit */
-
- mdp->self = mdp; /* self consistency, one simple way... */
-
- We don't do self consistency here like this, our system is more
- complex and allows us to type the objects etc.
-
- mdp->selected_w = my_select_widget;
-
- /* tell your selection box callbacks to use mdp as their client data*/
-
- c) Whenever referencing the memory cast it to a pointer to a structure
- of the type you have defined. For example in your callback routine.
-
- static XtCallbackProc my_callback(Widget w,
- XtPointer client_data,
- XmAnyCallbackStruct *str)
- {
- MY_DATA_PTR mdp = (MY_DATA_PTR)client_data;
- Widget list_w;
-
- list_w = XmSelectionBoxGetChild (mdp->selected_w, XmDIALOG_LIST_LABEL);
-
- /* etc... */
-
- return NULL; /* gcc is so fussy :-) */
- }
-
- End of lesson. No C++ needed, but you've got objects now. It ain't hard, and
- no globals! Anyone hiring?
- --
- snail@lsl.co.uk
-
- "Washing one's hands of the conflict between the powerful and the powerless
- means to side with the powerful, not to be Neutral."
- Quote by Freire.
- Poster by OXFAM.
-