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

Harakiri <[email protected]> Tue, 29 Jan 2013 07:46:26 -0800 (PST)
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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