Deleting global lua objects from C++

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

Deleting global lua objects from C++

gennadich
Hi!

I set a C++ object as a lua global, like this:

  Typek* k = new k();

  luabind::globals(pMyLuaState)["obj"] = k;


And now i would like to delete this global variable. Not just assign 0
to it, but delete it completely.

I mean, code like this:

        for (int i=0; i<100000000; i++)
        {
                char buf[1024];
                std::string st("t");
                st.append(itoa(i,(char*)buf,10));

                std::string luacode = st;
                luacode.append(":fire()");

                SomeTank* k = new SomeTank();

                luabind::globals(pMyLuaState)[st.c_str()] = k;
                lua_dostring(pMyLuaState,luacode.c_str());
                luabind::globals(pMyLuaState)[st.c_str()] = 0;
        }

eats memory..


--
Best Regards,
Ostvald Blumkenshtern
mailto:[hidden email]




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Deleting global lua objects from C++

Arvid Norberg-2
On Mar 31, 2006, at 10:54, gennadich wrote:

> Hi!
>
> I set a C++ object as a lua global, like this:
>
>   Typek* k = new k();
>
>   luabind::globals(pMyLuaState)["obj"] = k;
>
>
> And now i would like to delete this global variable. Not just assign 0
> to it, but delete it completely.
>
> I mean, code like this:
>
>         for (int i=0; i<100000000; i++)
>         {
>                 char buf[1024];
>                 std::string st("t");
>                 st.append(itoa(i,(char*)buf,10));
>
>                 std::string luacode = st;
>                 luacode.append(":fire()");
>
>                 SomeTank* k = new SomeTank();
>
>                 luabind::globals(pMyLuaState)[st.c_str()] = k;
>                 lua_dostring(pMyLuaState,luacode.c_str());
>                 luabind::globals(pMyLuaState)[st.c_str()] = 0;
>         }
>
> eats memory..

Code like that is probably not very suitable to be written in lua at  
all, because how the GC works. But anyway, you could write a delete  
function in C++ and export to lua (if you want to delete it from  
lua). If you are sure that there are no other instances of the  
reference to k in lua, you could just delete it before you assign 0  
to it.

Doing it this way of course won't use the GC in lua, and can  
potentially give you dangling pointers.

Another, maybe more secure way, would be to force a GC-cycle after  
you've assigned 0 to the variable. This may not be very efficient,  
but at least you won't have dangling pointers. See the lua manual for  
how to invoke the GC.

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re[2]: Deleting global lua objects from C++

gennadich
Hi, Arvid.

On Mar 31, 2006, at 14:55:51 Arvid wrote:

> On Mar 31, 2006, at 10:54, gennadich wrote:

> Code like that is probably not very suitable to be written in lua at
> all, because how the GC works. But anyway, you could write a delete
> function in C++ and export to lua (if you want to delete it from  
> lua). If you are sure that there are no other instances of the  
> reference to k in lua, you could just delete it before you assign 0
> to it.

> Doing it this way of course won't use the GC in lua, and can  
> potentially give you dangling pointers.

> Another, maybe more secure way, would be to force a GC-cycle after  
> you've assigned 0 to the variable. This may not be very efficient,  
> but at least you won't have dangling pointers. See the lua manual for
> how to invoke the GC.

> --
> Arvid Norberg

Thanks a lot for your reply.

I've read about GC, but this code:

        for (int i=0; i<100000000; i++)
        {
                char buf[1024];
                std::string st("t");
                st.append(itoa(i,(char*)buf,10));

                std::string luacode = st;
                luacode.append(":fire()");

                SomeTank* k = new SomeTank();

                luabind::globals(pMyLuaState)[st.c_str()] = k;

                //fire method of SomeTank is empty, it doesn't use memory
                lua_dostring(pMyLuaState,luacode.c_str());

                luabind::globals(pMyLuaState)[st.c_str()] = 0;

                lua_dostring(pMyLuaState,"collectgarbage()");

                delete k;
        }

still eats memory... Why?...

I just wanted to say, that while using some lua_State pointer I can
register C++ objects in Lua globals with luabind.

After lua_dofile procedure I want to continue using this pointer. C++
objects are already destroyed to this moment. (delete someobject;)
Lua global variables were set to 0 like this:

    luabind::globals(pMyLuaState)["someobject"] = 0;

But Lua doesn't unset them by itself, I mean lua record of variable
"someobject" is still alive, and
luabind::globals(pMyLuaState)["someobject"] variable can be accessed
(and it is 0).

But I don't need it anymore! I want lua to "forget about it"
completely.

Does GC do this work? Does it unsets such mmm... records? Seems like
it doesn't, or, maybe I'am invoking GC not properly?..


--
with Best Regards
Ostvald Blumkenshtern
mailto:[hidden email]




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Deleting global lua objects from C++

Arvid Norberg-2

On Mar 31, 2006, at 14:51, gennadich wrote:

> Hi, Arvid.
>
> On Mar 31, 2006, at 14:55:51 Arvid wrote:
>
>> On Mar 31, 2006, at 10:54, gennadich wrote:
>
>> Code like that is probably not very suitable to be written in lua at
>> all, because how the GC works. But anyway, you could write a delete
>> function in C++ and export to lua (if you want to delete it from
>> lua). If you are sure that there are no other instances of the
>> reference to k in lua, you could just delete it before you assign 0
>> to it.
>
>> Doing it this way of course won't use the GC in lua, and can
>> potentially give you dangling pointers.
>
>> Another, maybe more secure way, would be to force a GC-cycle after
>> you've assigned 0 to the variable. This may not be very efficient,
>> but at least you won't have dangling pointers. See the lua manual for
>> how to invoke the GC.
>
>> --
>> Arvid Norberg
>
> Thanks a lot for your reply.
>
> I've read about GC, but this code:
>
>         for (int i=0; i<100000000; i++)
>         {
>                 char buf[1024];
>                 std::string st("t");
>                 st.append(itoa(i,(char*)buf,10));
>
>                 std::string luacode = st;
>                 luacode.append(":fire()");
>
>                 SomeTank* k = new SomeTank();
>
>                 luabind::globals(pMyLuaState)[st.c_str()] = k;
>
>                 //fire method of SomeTank is empty, it doesn't use  
> memory
>                 lua_dostring(pMyLuaState,luacode.c_str());
>
>                 luabind::globals(pMyLuaState)[st.c_str()] = 0;
>
>                 lua_dostring(pMyLuaState,"collectgarbage()");
>
>                 delete k;
>         }
>
> still eats memory... Why?...

The userdata created by luabind to wrap the pointer is still not  
destroyed, because its lifetime is controlled by the GC. It is  
possible that collect garbage needs to be called twice before all of  
that state is cleaned up,

This code should at least eat less memory than before, right? since  
at least SomeTank is freed.

> I just wanted to say, that while using some lua_State pointer I can
> register C++ objects in Lua globals with luabind.
>
> After lua_dofile procedure I want to continue using this pointer. C++
> objects are already destroyed to this moment. (delete someobject;)
> Lua global variables were set to 0 like this:
>
>     luabind::globals(pMyLuaState)["someobject"] = 0;
>
> But Lua doesn't unset them by itself, I mean lua record of variable
> "someobject" is still alive, and
> luabind::globals(pMyLuaState)["someobject"] variable can be accessed
> (and it is 0).
>
> But I don't need it anymore! I want lua to "forget about it"
> completely.
>
> Does GC do this work? Does it unsets such mmm... records? Seems like
> it doesn't, or, maybe I'am invoking GC not properly?..

I'm not sure why the GC wouldn't clean that up. But you might want to  
call

void  lua_setgcthreshold (lua_State *L, int newthreshold);

instead of running it through lua.

You could also goole (or check the lua wiki) for a reference counting  
patch, which will help in deleting objects sooner.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user