chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Dynamic loading foreign C library problem


From: felix winkelmann
Subject: Re: [Chicken-users] Dynamic loading foreign C library problem
Date: Tue, 22 Nov 2005 07:35:08 +0100

Hi, Matthew!

How do you allocate the C string, and how is it passed
to Scheme?


cheers,
felix


On 11/22/05, Matthew David Parker <address@hidden> wrote:
>
>
> 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 127.0.0.1".  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.so xpai.c `chicken-config -shared -libs -cflags` -lgame_ai
> -shared -fPIC `sdl-config --libs --cflags`
>
> And it compiles fine, into xpai.so
>
> Then I run csi and (load "xpai.so") which it does fine.
>
> Then I run (game_load "-join termite.cs.indiana.edu")
>
> 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
> somehow.
>
> 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 libgame_ai.so C
> library, it doesn't properly handle the memory of the dynamic libraries
> that libgame_ai.so is linked to?
>
> Matt
>
>
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-users
>




reply via email to

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