comp.lang.ada
 help / color / mirror / Atom feed
* Free-ing memory: not springing leaks?
@ 2003-09-13 21:33 chris
  2003-09-14  1:10 ` Matthew Heaney
  0 siblings, 1 reply; 12+ messages in thread
From: chris @ 2003-09-13 21:33 UTC (permalink / raw)


Hi,

When you bind to c code, how do you deal with memory?  I bound to a 
function in Lua,

    function Lua_Version return ICS.Chars_Ptr;
    pragma Import (C, Lua_Version);

but upon free-ing the memory it gave a storage_error.

    function Version return String is
       X : ICS.Chars_Ptr := Lua_Version;
    begin
       declare
          Y : String := ICS.Value (X);
       begin
          ICS.Free (X);
          return Y;
       end;
    end Version;

To be clear, my understanding of this code is it get's a ptr to a null 
terminated string from Lua_Version, get's that value as a string, frees 
the null terminated string and returns the ada string.  Clearly, 
something is wrong with my understanding of how this works; if you free 
the memory it gives a storage error.

How then, do you deal with memory leaks?  If you bind to a c function 
and it allocates a chunk of memory and returns it, how do get rid of it 
and not cause leaks?  Remove Free and no error occurs.  Perhaps this is 
a special case?

I vaguely recall reading somewhere that in general you shouldn't free C 
allocated memory from Ada, but perhaps I was mistaken?


Can anyone please help with some advice or clarification?  I have only 
ever preallocated memory, passed it to a C function and released it 
afterwards (e.g. to pass strings into C functions) not had memory given 
by a c function and tried to release it.


Thanks,
Chris




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2003-09-19 13:07 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-13 21:33 Free-ing memory: not springing leaks? chris
2003-09-14  1:10 ` Matthew Heaney
2003-09-14 11:02   ` chris
2003-09-14 12:51     ` Matthew Heaney
2003-09-14 12:14       ` chris
2003-09-14 14:24         ` Ludovic Brenta
2003-09-14 19:00           ` chris
2003-09-14 16:06         ` Simon Wright
2003-09-15  7:13           ` Martin Krischik
2003-09-18 20:15         ` Warren W. Gay VE3WWG
2003-09-19  3:02           ` Hyman Rosen
2003-09-19 13:07             ` Warren W. Gay VE3WWG

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox