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: Matthew David Parker
Subject: Re: [Chicken-users] Dynamic loading foreign C library problem
Date: Tue, 22 Nov 2005 12:04:34 -0500 (EST)

On Tue, 22 Nov 2005, felix winkelmann wrote:
> Hi, Matthew!
>
> How do you allocate the C string, and how is it passed
> to Scheme?


Ah hah!  That was it, no fault with CHICKEN, really, I allocated the
string incorrectly.  I was doing:

AIargs = malloc(sizeof(argstring));

Which was only 4 bytes, because it was doing the size of the pointer,
argstring.  So I had to do sizeof(strlen(argstring) + 1) instead.

Now I fixed that and some other malloc'd things and the game is loading up
very well!  Thank you for your direction.  I like CHICKEN very much so
far.

Matt

PS. On a side question, is there some thing you can do in the csi chicken
interpreter to make it so you can press the "up" arrow and cycle through
the history of your commands, like in a bash shell?


>
>
> 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]