On 29 Dec 2005 at 13:51, Chris Hicks wrote:
[...]
I am also wondering whether the Pascal in the 2003 example was quite right. Does the second getmem release the memory? program pascalcallsC(input,output);
procedure world; external name 'c_world'; procedure w1(var i : integer; s : pchar); external name 'c_w1';
var i : integer; s : pchar; begin writeln("Hello world from Pascal"); world; writeln("Returned from C to Pascal\n"); i := 96; getmem (s, 260); { ahem - we need to allocate memory } w1(i,s); writeln("Returned from C again I = ",i); writeln("Return from C cstring = ",s); getmem (s, 260); { free this here, else memory leak on next line }
This is a memory leak. If you are trying to free the memory here, then what you should call is "freemem", not "getmem".
s := "This is a CString"; { not really a good idea, IMHO }
But of course, if you called "freemem" above (and even if you didn't), then it is indeed not a good idea to make the assignment just made. At best, you'd get a memory leak. At worst, you'd get a GPF.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/