chicken-users
[Top][All Lists]
Advanced

[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

Hello!

I have a C program and Scheme program embedded in it. I need to pass a
c-string
parameter to Scheme part of the program and get a c-string as the result. In
both
cases I don't know the size of the strings and can't estimate their maximum
length.
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.

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
are
to be called from Scheme to obtain input string and set result respectively
(theirs
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

    free(scm_output_string);
}

================== 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*
"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))

(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
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.

Best regards,
Andrey

=======================================
Andrey Fomichev (fomichev at ispras.ru)
Sedna Team (http://modis.ispras.ru)
=======================================






reply via email to

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