home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / ada / 2392 < prev    next >
Encoding:
Text File  |  1992-08-20  |  3.1 KB  |  93 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!usc!elroy.jpl.nasa.gov!jato!uccs!dsoulele
  3. From: dsoulele@uccs.jpl.nasa.gov (Dean Souleles)
  4. Subject: Memory leak in Ada/X bindings
  5. Message-ID: <1992Aug20.184211.1365@jato.jpl.nasa.gov>
  6. Originator: dsoulele@uccs
  7. Sender: nobody@jato.jpl.nasa.gov
  8. Nntp-Posting-Host: uccs.jpl.nasa.gov
  9. Organization: Jet Propulsion Laboratory, Pasadena, CA
  10. Date: Thu, 20 Aug 1992 18:42:11 GMT
  11. Lines: 80
  12.  
  13. In article <1992Aug14.144015.17168@u.washington.edu> sinan@milton.u.washington.edu (Sinan Karasu) writes:
  14. >
  15. >   
  16. >In the following code ( which is from Stars Xlib bindings) the lines
  17. >flagged by -- sjw are needed in an Alsys environment, 
  18. >since otherwise we end up a memory leakage problem .. What does LRM say 
  19. >about this? is this a bug or normal ? is there a simple way to take care 
  20. >of this? Any suggestions are heartily welcome.
  21. >-------------------------------------------------------------------------
  22. >
  23. >      function set_event
  24. >          (untyped_event_record : x_crossing_event_record) return event_record
  25. >          is
  26. >         untyped_event : x_event :=
  27. >            new x_crossing_event_record'(untyped_event_record);
  28. >
  29. >         procedure free is new unchecked_deallocation -- sjw
  30. >           (object => X_Crossing_Event_Record,        -- sjw
  31. >            name   => X_Event);                       -- sjw
  32. >
  33. >      begin
  34. >         case event_type'val (untyped_event.kind) is
  35. >            when button_press  =>
  36. >               declare
  37. >                  the_x_event : x_button_event :=
  38. >                     to_x_button_event (untyped_event);
  39. >               begin
  40. >                  free(untyped_event); --sjw
  41. >                  return
  42. .
  43. .
  44. .
  45. >  --- Thanx for your help
  46. >      Sinan
  47.  
  48.  
  49. This fix is incorrect.  The unchecked conversion: 
  50.  
  51.     the_x_event : x_button_event :=
  52.         to_x_button_event (untyped_event)
  53.  
  54. and all of the other similar ones, simply convert one acess type to another.
  55. After this unchecked conversion the variable the_x_event and untyped_event
  56. are access types that point to the SAME memory. You cannot then free the 
  57. untyped_event because it the memory is referenced via the the_x_event pointer
  58. in the return statements for each case.
  59.  
  60. Here is how I fixed the memory leak:
  61.  
  62.     function To_X_Event is new Unchecked_Conversion
  63.                                   (Source => System.Address, Target => X_Event);
  64.  
  65.     function Set_Event (Untyped_Event_Record : X_Crossing_Event_Record)
  66.                        return Event_Record is
  67.  
  68.         Untyped_Event : X_Event := To_X_Event(Untyped_Event_Record'address);
  69.  
  70.     begin
  71.         case Event_Type'Val (Untyped_Event.Kind) is
  72.             when Button_Press =>
  73.                 declare
  74.                     The_X_Event : X_Button_Event :=
  75.                        To_X_Button_Event (Untyped_Event);
  76.  
  77.  
  78. This eliminates the the "new" that was allocating the memory in the first
  79. place and does not leak.
  80.  
  81. This code has been implemented in the JPL Ada/Motif bindings.
  82.  
  83. Dean.
  84. --
  85. Dean Souleles
  86. dsoulele@uccs.jpl.nasa.gov
  87. Jet Propulsion Laboratory
  88. -- 
  89. Dean Souleles            dsoulele@uccs.jpl.nasa.gov
  90.                 dsoulele@dsfvax.jpl.nasa.gov
  91.  
  92. Jet Propulsion Laboratory
  93.