home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.ada
- Path: sparky!uunet!usc!elroy.jpl.nasa.gov!jato!uccs!dsoulele
- From: dsoulele@uccs.jpl.nasa.gov (Dean Souleles)
- Subject: Memory leak in Ada/X bindings
- Message-ID: <1992Aug20.184211.1365@jato.jpl.nasa.gov>
- Originator: dsoulele@uccs
- Sender: nobody@jato.jpl.nasa.gov
- Nntp-Posting-Host: uccs.jpl.nasa.gov
- Organization: Jet Propulsion Laboratory, Pasadena, CA
- Date: Thu, 20 Aug 1992 18:42:11 GMT
- Lines: 80
-
- In article <1992Aug14.144015.17168@u.washington.edu> sinan@milton.u.washington.edu (Sinan Karasu) writes:
- >
- >
- >In the following code ( which is from Stars Xlib bindings) the lines
- >flagged by -- sjw are needed in an Alsys environment,
- >since otherwise we end up a memory leakage problem .. What does LRM say
- >about this? is this a bug or normal ? is there a simple way to take care
- >of this? Any suggestions are heartily welcome.
- >-------------------------------------------------------------------------
- >
- > function set_event
- > (untyped_event_record : x_crossing_event_record) return event_record
- > is
- > untyped_event : x_event :=
- > new x_crossing_event_record'(untyped_event_record);
- >
- > procedure free is new unchecked_deallocation -- sjw
- > (object => X_Crossing_Event_Record, -- sjw
- > name => X_Event); -- sjw
- >
- > begin
- > case event_type'val (untyped_event.kind) is
- > when button_press =>
- > declare
- > the_x_event : x_button_event :=
- > to_x_button_event (untyped_event);
- > begin
- > free(untyped_event); --sjw
- > return
- .
- .
- .
- > --- Thanx for your help
- > Sinan
-
-
- This fix is incorrect. The unchecked conversion:
-
- the_x_event : x_button_event :=
- to_x_button_event (untyped_event)
-
- and all of the other similar ones, simply convert one acess type to another.
- After this unchecked conversion the variable the_x_event and untyped_event
- are access types that point to the SAME memory. You cannot then free the
- untyped_event because it the memory is referenced via the the_x_event pointer
- in the return statements for each case.
-
- Here is how I fixed the memory leak:
-
- function To_X_Event is new Unchecked_Conversion
- (Source => System.Address, Target => X_Event);
-
- function Set_Event (Untyped_Event_Record : X_Crossing_Event_Record)
- return Event_Record is
-
- Untyped_Event : X_Event := To_X_Event(Untyped_Event_Record'address);
-
- begin
- case Event_Type'Val (Untyped_Event.Kind) is
- when Button_Press =>
- declare
- The_X_Event : X_Button_Event :=
- To_X_Button_Event (Untyped_Event);
-
-
- This eliminates the the "new" that was allocating the memory in the first
- place and does not leak.
-
- This code has been implemented in the JPL Ada/Motif bindings.
-
- Dean.
- --
- Dean Souleles
- dsoulele@uccs.jpl.nasa.gov
- Jet Propulsion Laboratory
- --
- Dean Souleles dsoulele@uccs.jpl.nasa.gov
- dsoulele@dsfvax.jpl.nasa.gov
-
- Jet Propulsion Laboratory
-