[Top][All Lists]

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

[Chicken-users] Dynamic loading foreign C library problem

From: Matthew David Parker
Subject: [Chicken-users] Dynamic loading foreign C library problem
Date: Mon, 21 Nov 2005 18:32:38 -0500 (EST)

Hi, I have a game that is compiled as a shared library.  It uses X11 and
runs in a thread from SDL.

So normally I would link to this library and run "game_load(<args>)" where
<args> are command line options, like "-join".  calling
"game_load" will start the game in the thread and return control to the
program that called game_load.  I'm making an AI interface to the game, so
after it loads you can call functions like "move(32, 4)" to get the bot in
the game to move, or something similar.

I've gotten this to work dynamically linked from a C program, and I got it
to work in Chez scheme.  I'm now trying to use CHICKEN to do it, since
chicken is open source and chez isn't.

So, in CHICKEN I have a file xpai.scm that is supposed to interface to my
game.  Here's a simplified version of what's in it:

(foreign-declare "
#include \"game_ai.h\"")

(define game.load
        (foreign-lambda void "game_load" c-string))

I compile it like this:
> chicken xpai.scm -dynamic

> gcc -o xpai.c `chicken-config -shared -libs -cflags` -lgame_ai
-shared -fPIC `sdl-config --libs --cflags`

And it compiles fine, into

Then I run csi and (load "") which it does fine.

Then I run (game_load "-join")

the game_load function in C mallocs the arg string permanately and AIargs
points to it.

I can printf AIargs at this point and it's fine.

Then, I run SDL_Init, and if I printf AIargs it's just "-join termit"
instead of the full string it once was, as if SDL_Init cut into the memory

I read SDL_Init sometimes causes problems on Mac OS X (even though I'm
using linux 2.6.8), so I moved SDL_Init to a spot in the code before I
malloc'd AIargs.

THe program got a little further this time; it connected to the server,
but then there as a segmentation fault, I think while it was trying to load
the X window for the game graphics. I could have traced it down to the
exact line, but I figure there's just some option in CHICKEN that I need
to set so that it can properly handle this.

Maybe the problem is that, while chicken properly loads my C
library, it doesn't properly handle the memory of the dynamic libraries
that is linked to?


reply via email to

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