home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / windows / x / motif / 8810 < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.3 KB  |  102 lines

  1. Path: sparky!uunet!mcsun!uknet!lsl!snail
  2. From: snail@lsl.co.uk
  3. Newsgroups: comp.windows.x.motif
  4. Subject: Re: XmSelectionBoxGetChild crashes application
  5. Message-ID: <1993Jan25.173251.2979@lsl.co.uk>
  6. Date: 25 Jan 93 17:32:51 GMT
  7. References: <5935@esf.esf.de> <1993Jan22.164824@mccall.com>
  8. Organization: Laser-Scan Ltd., Cambridge
  9. Lines: 91
  10.  
  11. In article <1993Jan22.164824@mccall.com>, tp@mccall.com (Terry Poot) writes:
  12. > In article <5935@esf.esf.de>, klaus@tat2.esf.de (Klaus Wicovsky) writes:
  13. >>----------------------------------------------------------------------
  14. >>void fs_okCB(w, client_data, call_data)
  15. >>...
  16. >>...
  17. >>         text=XmSelectionBoxGetChild (w, XmDIALOG_TEXT);
  18. >>    filename=XmTextGetSelection(text);
  19. >>...
  20. >>    list = XmSelectionBoxGetChild (selected, XmDIALOG_LIST_LABEL);
  21. >>----------------------------------------------------------------------
  22. >>
  23. >>Whenever this callback procedure is carried out, my application crashes at
  24. >>the last line. "w" would be my original widget for which I define this
  25. >>callback
  26. >>and "selected" would be my new widget, which would like to modify.
  27. >>
  28. >>Although "selected" is globally defined, the last statement crashes my
  29. >>application. Any  idea why this could happen ???
  30.  
  31. You don't need global variables. Globals are poor engineering. See below.
  32.  
  33. > Happened to me last week. If you check, I think you'll find that w is not the
  34. > widget id of the selection box, but of the OK button. XmSelectionBoxGetChild
  35. > appears not to check for the widget id actually being a selection box, so it
  36. > crashes with a memory access error. I don't know if XtParent would reliably get
  37. > you the widget id of the selection box, it depends on how Motif bundles all that
  38. > stuff together. You need to remember the widget id of the selection box by some
  39. > other method so it will be accessible to your callback.
  40.  
  41. The last sentence....
  42.  
  43. client_data should be used for this.
  44. For example:-
  45.  
  46.     a) Data structures
  47.  
  48. struct my_data {
  49.     MY_DATA_PTR    self;
  50.  
  51.     Widget        selected_w;
  52.  
  53.     other data....
  54. };
  55.  
  56. typedef struct my_data *MY_DATA_PTR;
  57.  
  58.     b) Allocate some memory the size of a structure you have defined.
  59.  
  60. MY_DATA_PTR    mdp;
  61.  
  62. mdp = (MY_DATA_PTR)malloc(sizeof(struct my_data));
  63. if (mdp == NULL)
  64.             /* oh shit */
  65.  
  66. mdp->self = mdp;    /* self consistency, one simple way... */
  67.  
  68.     We don't do self consistency here like this, our system is more
  69.     complex and allows us to type the objects etc.
  70.  
  71. mdp->selected_w = my_select_widget;
  72.  
  73.     /* tell your selection box callbacks to use mdp as their client data*/
  74.  
  75.     c) Whenever referencing the memory cast it to a pointer to a structure
  76.     of the type you have defined. For example in your callback routine.
  77.  
  78. static XtCallbackProc my_callback(Widget w,
  79.                                   XtPointer client_data,
  80.                                   XmAnyCallbackStruct *str)
  81. {
  82.     MY_DATA_PTR    mdp = (MY_DATA_PTR)client_data;
  83.     Widget        list_w;
  84.  
  85.     list_w = XmSelectionBoxGetChild (mdp->selected_w, XmDIALOG_LIST_LABEL);
  86.  
  87.     /* etc... */
  88.  
  89.     return NULL;    /* gcc is so fussy :-) */
  90. }
  91.     
  92. End of lesson. No C++ needed, but you've got objects now. It ain't hard, and
  93. no globals! Anyone hiring?
  94. -- 
  95. snail@lsl.co.uk      
  96.  
  97. "Washing one's hands of the conflict between the powerful and the powerless
  98.  means to side with the powerful, not to be Neutral."
  99.                                                      Quote by Freire.
  100.                                                      Poster by OXFAM.
  101.