[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] avoiding memory leaks

From: felix winkelmann
Subject: Re: [Chicken-users] avoiding memory leaks
Date: Tue, 25 Oct 2005 07:35:01 +0200

On 10/21/05, Andrey Fomichev <address@hidden> wrote:

> Below I have a solution that works fine as far as I tested, but I'm
> wondering if
> their present any memory leaks in my program. I would very appreciate if
> somebody
> could take a look at my solution and tell his/her opinion.

No, I can't see any memory leaks here.

> (declare (foreign-declare "extern char* get_scm_input_string();"))
> (define get-scm-input-string (foreign-callback-lambda c-string*
> "get_scm_input_string"))
> (declare (foreign-declare "extern void set_scm_output_string(char* s);"))
> (define set-scm-output-string (foreign-callback-lambda void
> "set_scm_output_string" c-string))

These don't need to be callback-lambdas. using `foreign-lambda' might
be more efficient.

> I'm a bit confused about function set_scm_output_string. Because in Scheme
> this
> function is declared as function with parameter c-string, I thought that its
> body
> should be the following
> void  set_scm_output_string(char* s)
> {
>     scm_output_string = s;
> }
> But this doesn't work. My program hangs up.

set_scm_output_string gets passed a pointer to Scheme data. If you
keep the pointer, and a GC occurs, the pointer won't be valid any more.
You should copy it's contents to a safe (static) area before returning
to Scheme.

Another solution is GC hooks: you can store references to Scheme
data in a GC-hook created with CHICKEN_new_gc_root(). Store the
reference with CHICKEN_gc_root_set() and retrieve it via
CHICKEN_gc_root_ref(). To access the actual string data, use
C_header_size() and C_c_string (or C_data_pointer).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]