[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Memory Leaks with foreign libraries
From: |
felix winkelmann |
Subject: |
Re: [Chicken-users] Memory Leaks with foreign libraries |
Date: |
Fri, 3 Feb 2006 08:17:41 +0100 |
On 2/2/06, Matthew David Parker <address@hidden> wrote:
>
Hi, Matthew!
> Then in my game loop, which is all in the compiled game library that was
> written in C, I have an AI_loop() function that gets called once every
> gameplay frame. At the bottom of this it calls "callback();"
>
> Then in chicken I have defined:
> (define-external (callback) void
> (thread-yield!))
>
> So this way I can play the game and enter commands into chicken at the
> same time.
It is absolutely essential, that the foreign-procedure that invokes
the code the eventually performs the callback is a `foreign-safe-lambda[*]'.
>
> There is a memory leak, though, and it leaks even when the ship is just
> sitting there on the screen with no calls to "malloc" or "free". I am
> wondering if it has something to do with the callback, because, as it is
> written:
> "Non-local exits leaving the scope of the invocation of a callback from
> Scheme into C will not remove the C call-frame from the stack (and will
> result in a memory leak)."
That means, if you are in Scheme code (called by C) and invoke a continuation
that was captured before calling the C code, then you will leak space.
> I also have my own TCP implementation written in C that that I use in
> Chicken. That one has some memory leaks as well, though it doesn't have a
> callback function. It does a lot of "malloc" and "free" but it works
> without a leak if you use it with a C program. I don't know what the leak
> there could be. What about passing strings between C and scheme? Do you
> have to do something special to allocate new space for strings in chicken?
>
> I have a lot of functions that I call from scheme into C, where C returns
> a string, and they are just normal C functions that return some pointer in
> memory to a string, then I access them through chicken with this:
> (define AI.msg.body?
> (foreign-safe-lambda c-string "FI_AImsg_body" int))
>
> So I'd just call (AI.msg.body?) and it would return some string.
The content of the char * returned by the C procedure will be copied
into a Scheme string, so, unless the C code expects the returned
string to be free()'d, nothing should leak (this means the returned
C string must be in static memory, not malloc'd).
cheers,.
felix