Re: Dangling pointers Lua style...

Nigel Atkinson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <1285887146.1610.30.camel@octavo>
Yes, that would work. Thank you.  

I was trying to think of a generic solution, such as some sort of
"ward_ptr" that threw an exception on getPointer() if it was invalid.
However the problem is - how would such a class "know" when the pointer
it contains has been free'd.  It probably could be done via, heap
debugging functions or some such, but that's just yuck. (Slow,
non-portable, etc)

A wrapper class it is methinks!  But hang on moment...
would this work??  How clever is Luabind about this...

void Destroy( ward_ptr<myclass> ptr )
{
	ptr->Destroy();
	ptr.invalidate();
}

class_<myclass, ward_ptr>("myclass")
[
	.def("Create", MakeOne ) // MakeOne would return a myclass*
	bla bla bla
	.def("Destroy", Destroy )
]

Hmmmmm.

Nigel

On Thu, 2010-09-30 at 13:17 -0400, Evan Wies wrote:
> You can try wrapping the Create/Destroy in a proxy class that uses 
> constructors/destructors instead?  It depends on how pervasive this 
> idiom is in your code.  From there, Luabind is pretty flexible with how 
> you can bind arbitrary functions to classes.
> 
> If you want to explicitly control lifetimes, check out how Corona deals 
> with a similar problem:
> http://developer.anscamobile.com/content/application-programming-guide-graphics-and-drawing#Removing_Objects_Properly
> 
> -Evan
> 
> 
> On 09/30/2010 09:28 AM, Nigel Atkinson wrote:
> > Consider a bound class without a bound constructor.  Instead elsewhere
> > there is a bound function to create and another to destroy.
> >
> > Used something like this...
> >
> > obj = CreateObject()
> >
> > -- use the obj
> >
> > DestroyObject( obj )
> >
> > The destroy might do more, but ultimately deletes the object.  Problem
> > is in Lua, we now have a dangling pointer!  obj after the destroy call
> > still points somewhere, and if you accidentally use it bad things
> > happen.  Not good if the Lua scripts are from the end user.  They can't
> > be expected to write "obj = nil" after each call. :)
> >
> > So how to automatically set obj to nil after a destroy call?
> >
> > My thoughts lean towards “All problems in computer science can be solved
> > by another level of indirection”
> >
> > In this case some sort of smart/auto pointer, and wrapper functions that
> > throw exceptions if the passed pointer points to bad data?
> >
> > What are everyone's thoughts?  I have the feeling I've missed some
> > obvious answer.
> >
> >
> > ------------------------------------------------------------------------------
> > Start uncovering the many advantages of virtual appliances
> > and start using them to simplify application deployment and
> > accelerate your shift to cloud computing.
> > http://p.sf.net/sfu/novell-sfdev2dev
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> 
> 
> 
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.