Derived Lua classes are garbage collected

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Derived Lua classes are garbage collected

Pablo Palmier Campos
Hi to all,

I define a LUA class that inherits from another LUA class, this one
inheriting from a C++ class, create an instance of it in C++ and adopt
the result. After the garbage collector does its work, if I invoke a
method on this object I get:

Assertion failed: !lua_isnil(L, -1), file c:\documents and settings\pablo\mis do
cumentos\computer science\master de videojuegos\project\luabindtester\luabindtes
ter\luabindtester\inc\luabind\wrapper_base.hpp, line 122

So it appears that some luabind objects bound to the LUA class
hierarchy are garbage collected. I'm using Luabind 0.7 and Lua 5.0.2.
The following code demonstrates the problem (forcing the execution of
the garbage collector in checkIfKeptAlive()). Note that the crash does
not occur if the object is an instance of a LUA class that directly
inherits from a C++ class.

===
C++
===

class A {
public:
    virtual ~A() {
    }

    virtual void print(unsigned int i) {
        cout << "A counts: " << i << endl;
    }
};

class AWrapper : public A, public luabind::wrap_base {
public:
    virtual ~AWrapper() {
    }

    virtual void print(unsigned int i) {
        call<void>("print", i);
    }

    static void default_print(A *pA, unsigned int i) {
        pA->A::print(i);
    }
};

(...)

        module(L)
        [
                class_<A, AWrapper>("A")
                        .def(constructor<>())
                        .def("print", A::print, AWrapper::default_print)
        ];

(...)

static void
checkIfKeptAlive(lua_State *L, A *pA) {
        for (unsigned int i = 0; i < 4; ++i) {
                lua_setgcthreshold(L, 0);
                pA->print(i);
        }
}

(...)

        // --- B (LUA) inherits from A (C++)
        // Works perfectly!
        A *pA = call_function<A *>(L, "B")[adopt(result)];
        checkIfKeptAlive(L, pA);
        delete pA;

        // --- C (LUA) inherits from B (LUA)
        // Crashes
        pA = call_function<A *>(L, "C")[adopt(result)];
        checkIfKeptAlive(L, pA);
        delete pA;

===
LUA
===

class 'B' (A)

function B:__init()
   super();
end

function B:print(count)
   print('B counts ' .. count);
end

class 'C' (B)

function C:__init()
   super();
end

function C:print(count)
   print('C counts ' .. count);
end

===

Is there any problem in the code? Any suggestions or advices would be
greatly appreciated. Thank you all in advance,

Pablo Palmier

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user