I have some callback manager on C++ side (which stores
luabind::object), and I'm subscribing to it callbacks, which I create
inside some of my Lua coroutines.
Then that coroutine gets garbage collected, and I'm getting a crush in
Luabind when I'm trying to call the callback, or even to replace it
with new one.
I think the problem is triggered by following:
Luabind stores callback's state inside luabind::object. That state for
objects, created inside some coroutine is that coroutine itself. After
coroutine is garbage collected, that Lua state is no longer valid.
When Luabind tries to access the object (either to call it, or to
destroy it), it triggers access violation when calling Lua functions
with that state as parameter.
Am I missing something? What can I do to workaround this problem now?
Please see full source code below.
Thanks in advance,
===== coro.lua =====
-- Some C++-side callback manager
local callback_man = callback_man_t()
-- Some coroutine function
local coro_fn = function()
-- Our callback, declared as local variable here
local callback = function()
print("callback is called")
-- Subscribe our callback
-- Create coroutine
local coro = coroutine.create(coro_fn)
-- Resume coroutine so that callback is subscribed
-- Note that the coroutine status here is "dead"
coro = nil
-- No more references for coro object, so it gets garbage collected here
callback_man:call_callback() -- And we get a crash here
-- Or even here
callback_man:set_callback(function() print("new callback") end)
===== /coro.lua =====
Sounds like that person was trying to do the same thing, and the
solution appears to be, to use the MAIN lua_state in further calls, not
the lua_state of the co-routine. (because the lua_states share the
Now, I don't THINK you can just CHANGE the state of the luabind::object
(though you might try). Instead, maybe you could create a new object
that uses the main state, but the co-routine's callback.
Failing all that mucking around, you might be better off keeping a list
(in lua) of all the functions you have submitted as callbacks to your C
code, then return this list after any yields.. or the final return
statement of the co-routine, that way the objects SHOULD definitely
remain in scope.
It might even come down to a mixture of both methods now that I think
Well, I hope I've helped out a bit, even with all my ramblings!
From: [hidden email] [mailto:[hidden email]] On Behalf Of
Sent: Thursday, 8 June 2006 5:37 AM
To: [hidden email] Subject: Re: [luabind] Trouble with coroutines and callbacks
> I have some callback manager on C++ side (which stores
> luabind::object), and I'm subscribing to it callbacks, which I create
> inside some of my Lua coroutines.
> Then that coroutine gets garbage collected, and I'm getting a crush in
> Luabind when I'm trying to call the callback, or even to replace it
> with new one.
Can anyone give me any hints on this problem? Can any one reproduce
it? Am I doing something incredibly silly? Should I RTFM better?
I'm a bit frustated by the lack of replies to my recent posts in this