chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] FFI with callbacks for gnu readline


From: Thomas Chust
Subject: Re: [Chicken-users] FFI with callbacks for gnu readline
Date: Thu, 19 Jan 2006 00:22:19 +0000 (GMT)

On Wed, 18 Jan 2006, LeviPearson wrote:

The readline library expects to be able to call a generator function with a string and an index and get back a newly-allocated string that it can use and then free(). To accommodate this, I wrote a C function that calls a define-external'd scheme function that returns a scheme-object containing the string. I found that returning a c-string would sometimes give me some garbage characters as the strings were not always null-terminated at the right spot.

Hello,

actually I doubt that the termination of strings with a '\0' character is broken. Maybe the problem is rather related to garbage collection somehow, if the string is used for a longer time by the readline library. But nevertheless your approach here looks sensible.

[...] This all seems to work well, but after a bit of use of the tab-completion functionality, the program segfaults or otherwise dies on a call to the readline interface.

Well, this is no wonder, because your function gnu-readline is a foreign-lambda* and not a foreign-safe-lambda*. Callbacks into Scheme are only safe inside foreign-safe-lambda and friends, otherwise your stack may unexpectedly be sweeped clean by a minor garbage collection and your C code won't like that very much.

So just replace the foreign-lambda* by foreign-safe-lambda* in your readline.scm for every function that may eventually cause a callback intro Scheme and everything should be fine.

[...]

cu,
Thomas




reply via email to

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