Re: Protecting memory
eduard mueller <[email protected]> Thu, 1 Dec 2011 17:02:31 +0100
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CAMCDzW2meueCkNEkfvq0PG4Xgy+TGHytkEd8fQtX2YomNgFsTA@mail.gmail.com> |
See also "Dangling pointers Lua style" -> http://old.nabble.com/Dangling-pointers-Lua-style...-to29848215.html#a29853492 The only thing which we never could get done is setting such invalid C++ pointers to nil in Lua, as soon as they get invalid/NULL in C++. Without this, you either have to throw an error in the get_pointer() accessor as shown in the linked post, and need to add a helper function to Lua which allows checking for nil. Like "is_valid_object()" which does not use the get_pointer() wrapper and then checks for NULL with the smart pointer in C++. For scripts this is not really ideal, because you do expect an object can be compared to "nil" in order to check if it's valid. But well, it works, and will ensure that access to dead object never causes an access violation... Best regards, Eduard On Thu, Dec 1, 2011 at 16:06, Ryan Pavlik <[email protected]> wrote: > This seems like a good time to use boost's shared_ptr and weak_ptr. > > Ryan > > > On Wed, Nov 30, 2011 at 4:27 PM, Nigel Atkinson <[email protected]> > wrote: >> >> Hi Colin, >> >> You can wrap your pointers up in a smart pointer, made specifically >> for the job. I made what I call a ward_ptr, a class that is really >> just a wrapper around a raw pointer, except it has a member called >> 'invalidate' which sets the raw pointer it stores to null. You call >> this on the C++ side whenever the object it points to is deleted. If >> the smart pointer is used (dereferenced) while its stored pointer is >> null, it throws an exception - which if that happens inside a function/ >> method called from luabind, gets turned into a lua error by luabind. >> >> You could have a "isvalid" method too, in order to check a pointer >> rather than just using it and causing an exception. I don't in mine, >> as I use it with a library I didn't write bound with luabind. >> >> Heres a link to some code! Feel free to use it. >> >> https://github.com/merlinblack/Game-Engine-Testbed/blob/master/include/ward_ptr.h >> >> >> Nigel >> >> On 30/11/2011, at 10:39 PM, Colin GILLE / congelli501 wrote: >> >> > Hi, >> > >> > I'm new to this mailing list and quite new to luabind too. >> > I'm creating a 2D game engine with lua scripting and I wich to >> > protect the memory (against segmentation faults). >> > Some of my functions return pointers to objects, and these objects >> > can be deleted later by the engine. >> > When the user uses a pointer, I would like to check if this pointer >> > is still valid before using it in my C++ code. >> > I can easily do that if the pointer in a function argument but I >> > don't know how to that when the user use the member of the object. >> > >> > What is the good way to do it with luabind ? >> > >> > Thanks in advance ! >> > >> > ------------------------------------------------------------------------------ >> > All the data continuously generated in your IT infrastructure >> > contains a definitive record of customers, application performance, >> > security threats, fraudulent activity, and more. Splunk takes this >> > data and makes sense of it. IT sense. And common sense. >> > >> > http://p.sf.net/sfu/splunk-novd2d_______________________________________________ >> > luabind-user mailing list >> > [email protected] >> > https://lists.sourceforge.net/lists/listinfo/luabind-user >> >> >> >> ------------------------------------------------------------------------------ >> All the data continuously generated in your IT infrastructure >> contains a definitive record of customers, application performance, >> security threats, fraudulent activity, and more. Splunk takes this >> data and makes sense of it. IT sense. And common sense. >> http://p.sf.net/sfu/splunk-novd2d >> _______________________________________________ >> luabind-user mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/luabind-user > > > > > -- > Ryan Pavlik > HCI Graduate Student > Virtual Reality Applications Center > Iowa State University > > [email protected] > http://academic.cleardefinition.com > > ------------------------------------------------------------------------------ > All the data continuously generated in your IT infrastructure > contains a definitive record of customers, application performance, > security threats, fraudulent activity, and more. Splunk takes this > data and makes sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-novd2d > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-novd2d _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user