Help with adopt and Lua/C++ ownership

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with adopt and Lua/C++ ownership

Sean Barton
Hello all.  I've got a derived class in Lua that I want to have garbage collected but I'm obviously not doing something right.  I'm guessing the adopt policy gives me what I want but I haven't had any luck with it yet.

Here is my base C++ class:

    class Foo
    {
    public:
        virtual ~Foo()
        {
            printf("Good-bye cruel world!\n");
        }

        virtual void Greeting ()
        {
            printf("Hello World\n");
        }
    };

And here is the C++ Lua wrapper for it:

    class FooWrapper : public Foo, public luabind::wrap_base
    {
    public:
        virtual void Gretting()
        {
            call<void>("Greeting");
        }
        static void static_Greeting(Foo* foo)
        {
            foo->Foo::Greeting();
        }
    };

This is how I register this class in C++ for lua binding:

    module(lstate)
    [
        class_<Foo, FooWrapper>("Foo")
            .def(constructor<>())
            .def("Greeting", &Foo::Greeting, &FooWrapper::static_Greeting)
    ];

And here is the derived Lua class:

class 'MyFoo' (Foo)

function MyFoo:__init() super ()
    print("MyFoo:__init called!")
end

function MyFoo:Greeting()
    print("Hello, Luabind World!")
end

function MyFoo:__finalize()
    print("MyFoo:__finalize() called!")
end

And finally, here I am invoking all this in Lua script:

myTest = MyFoo()
myTest:Greeting()
myTest = nil
collectgarbage()

Here's the output from all this:

MyFoo:__init called!
Hello, Luabind World!


I would expect __finalize() on the lua-derived MyFoo class to be called as well as the original C++ Foo destructor, yet neither is happening.  Can someone clue me in to what I'm missing?

Thanks,
-seanba


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Help with adopt and Lua/C++ ownership

Arvid Norberg-2

On Sep 24, 2006, at 06:44, Sean Barton wrote:

> Hello all.  I've got a derived class in Lua that I want to have  
> garbage collected but I'm obviously not doing something right.  I'm  
> guessing the adopt policy gives me what I want but I haven't had  
> any luck with it yet.
> [...]

> And finally, here I am invoking all this in Lua script:
>
> myTest = MyFoo()
> myTest:Greeting()
> myTest = nil
> collectgarbage()
>
> Here's the output from all this:
>
> MyFoo:__init called!
> Hello, Luabind World!
>
>
> I would expect __finalize() on the lua-derived MyFoo class to be  
> called as well as the original C++ Foo destructor, yet neither is  
> happening.  Can someone clue me in to what I'm missing?

They aren't even called when you close the lua state? Usually you  
would have to call collectgarbage() twice before the instance is  
collected (this is because there's a double reference, and the first  
time the table is collected, and the second time the actual object,  
no longer held by the table, is collected).

--
Arvid Norberg



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Help with adopt and Lua/C++ ownership

Sean Barton
Thanks for thre response - I totally forgot to close the lua state when I was done!  So yes, now the __finalizer and the C++ destructor are called upon application exit. 

It's too late for my needs but it is a start.  I tried multiple calls to collectgarbage() but that didn't help (good thing to know anyhow).



On 9/24/06, Arvid Norberg <[hidden email]> wrote:

On Sep 24, 2006, at 06:44, Sean Barton wrote:

> Hello all.  I've got a derived class in Lua that I want to have
> garbage collected but I'm obviously not doing something right.  I'm
> guessing the adopt policy gives me what I want but I haven't had
> any luck with it yet.
> [...]

> And finally, here I am invoking all this in Lua script:
>
> myTest = MyFoo()
> myTest:Greeting()
> myTest = nil
> collectgarbage()
>
> Here's the output from all this:
>
> MyFoo:__init called!
> Hello, Luabind World!
>
>
> I would expect __finalize() on the lua-derived MyFoo class to be
> called as well as the original C++ Foo destructor, yet neither is
> happening.  Can someone clue me in to what I'm missing?

They aren't even called when you close the lua state? Usually you
would have to call collectgarbage() twice before the instance is
collected (this is because there's a double reference, and the first
time the table is collected, and the second time the actual object,
no longer held by the table, is collected).

--
Arvid Norberg



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user