Informing luabind when my c++ created objects get deleted?

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

Informing luabind when my c++ created objects get deleted?

Harakiri
My objects are only managed from C++, there is no creation/deletion in lua.

I exposed an actor class to lua using luabind, i then call specific lua functions with an actor object as parameter

function do_something(actor)
...
end

Im only calling these functions as long as my actor object exists.

The problem can arise if a scriptwriter doesnt know about object lifetime.

local actorRef

function do_something_for_actor1(actor)
  actorRef = actor
end

function do_something_for_actor2(actor)
   print "I want to use actor1 " .. actorRef:GetName()
end

Bad code aside which i cannot control, and numerous workarounds to tell script writers to use an ID instead of objects - when do_something_for_actor1 is called, and then later actor1 gets deleted the lua var actorRef points to invalid data.

So when for actor2  do_something_for_actor2 is called - i get an access violation.

Things i know:

- I can catch access violations, but its not what i want.
- I can create a proxy object and expose it with luabind the proxy object basically wraps the original object and on each method call checks if(object!=NULL) do.... - the issue is i would have to keep this proxy object forever - and the memory consumption would rise
- I scan through all upvalues for all functions i want to execute and set my userdata objects to NIL - this way there is only a non critical scripting error - no access violation.

I would like to make this work with a proxy object - however - i believe luabind:object is already a proxy object.

Can i somehow tell luabind that im about to delete my actor1 object and it automatically clears references in upvalues ? Im not using shared ptr or anything, just normal pointers and delete the data. Otherwise using a shared_ptr would result in that lua keeps my references for ever as long as actor2 lives (just an example)

Thanks

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Informing luabind when my c++ created objects get deleted?

Willi Schinmeyer
On 1/29/2013 4:46 PM, Harakiri wrote:
> My objects are only managed from C++, there is no creation/deletion in lua.
Well, that inherently comes with the problems you mentioned I guess. Why
do they need to be deleted at a fixed time then? Why not let Lua take
care of it?
> - I can create a proxy object and expose it with luabind the proxy
> object basically wraps the original object and on each method call
> checks if(object!=NULL) do.... - the issue is i would have to keep
> this proxy object forever - and the memory consumption would rise
That's what I would've suggested as well. Some kind of proxy with a weak
pointer. Why would that proxy be alive forever though? Surely it could
be deleted via Lua's garbage collection since it's only used in Lua?

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Informing luabind when my c++ created objects get deleted?

Harakiri



> That's what I would've suggested as well. Some kind of proxy
> with a weak
> pointer. Why would that proxy be alive forever though?
> Surely it could
> be deleted via Lua's garbage collection since it's only used
> in Lua?

Im creating the ProxyObject in C++, so i would assume that even using luabind - lua wouldnt try to delete it.

I give the ProxyObject as an arg to my lua functions, somebody could store it as an upvalue somewhere.

When does lua garbage collect it ? As long as there is still a lua function that wants to access this "empty proxy object" - i doubt it would be garbage collected.

Somebody said you could somehow force it with extending lua _gc table and there do a check everytime the gc runs like if(proxyObject:object==null) // delete somehow the proxyObject

Im not sure tho how this works if you have multiple references to the same object from different env scopes.

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Informing luabind when my c++ created objects get deleted?

Willi Schinmeyer
Lua can delete your objects just fine. You can even have weak pointers
in Lua (that don't keep objects from being garbage collected) by using
weak tables.

Lua uses reference counting - when the garbage collector runs and there
are no more (non-weak) references to the value, it will be deleted.
luabind::object-references count as well.

And yes, once you've passed that object into Lua it could potentially be
alive forever - it could be saved as a global variable, for example.
That's why you'll likely need a proxy object in the first place if you
want to control lifetime from C++.

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Informing luabind when my c++ created objects get deleted?

Harakiri-2


--- On Tue, 1/29/13, Willi Schinmeyer <[hidden email]> wrote:
>
> And yes, once you've passed that object into Lua it could
> potentially be
> alive forever - it could be saved as a global variable, for
> example.
> That's why you'll likely need a proxy object in the first
> place if you
> want to control lifetime from C++.


And thats exactly the reason why i prefer the approach of checking all upvalues before executing a luafunction through lua/bind, if any of these upvalues are userdata of a deleted object, i want to NIL them - i got to that part - but i still dont know how to cast a luabind userdata object back to my original one - see my other message - i need to know what kind of luabind is using to wrap my classes when they are exposed to lua

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user