[Top][All Lists]

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

[Chicken-users] avoiding memory leaks

From: Andrey Fomichev
Subject: [Chicken-users] avoiding memory leaks
Date: Fri, 21 Oct 2005 13:39:38 +0400


I have a C program and Scheme program embedded in it. I need to pass a
parameter to Scheme part of the program and get a c-string as the result. In
cases I don't know the size of the strings and can't estimate their maximum
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
could take a look at my solution and tell his/her opinion.

In C part of my program I have two char* variables:
* scm_input_string (I should fill it before calling Scheme)
* scm_output_string (here I can find result after Scheme part execution)

I have two functions get_scm_input_string and set_scm_output_string which
to be called from Scheme to obtain input string and set result respectively
body are below). And I have my_function which encapsulates my program.

================== C part of the program: BEGIN

static char *scm_input_string = NULL;
static char *scm_output_string = NULL;

char* get_scm_input_string()
    return scm_input_string;
void  set_scm_output_string(char* s)
    int size = strlen(s);
    scm_output_string = (char*)malloc(size + 1);
    strcpy(scm_output_string, s);

void my_function()
    scm_input_string = (char*)malloc(query_in_LR.size() + 1);

    // fill scm_input_string

    // initialize Chicken
    // call CHICKEN_eval_string (actually, call process-query-in-scheme)
    // result will be in scm_output_string

    // use scm_output_string according to program logic


================== C part of the program: end

================== Scheme part of the program: BEGIN

(declare (foreign-declare "extern char* get_scm_input_string();"))
(define get-scm-input-string (foreign-callback-lambda c-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))

(define (process-query-in-scheme)

  ; call get-scm-input-string and obtain input parameter

  ; process imput string

  ; call set-scm-output-string to set the output string



I'm a bit confused about function set_scm_output_string. Because in Scheme
function is declared as function with parameter c-string, I thought that its
should be the following

void  set_scm_output_string(char* s)
    scm_output_string = s;

But this doesn't work. My program hangs up.

Best regards,

Andrey Fomichev (fomichev at
Sedna Team (

reply via email to

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