Re: Protecting memory
"Colin GILLE / congelli501" <[email protected]> Sun, 4 Dec 2011 12:09:35 +0100
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CADT9g3YqTO4sxLtU7Me7eo05GUF5n_=nyT_x0aUOWfuvN0aVig@mail.gmail.com> |
Thanks for your answers !. I was able to protect the pointers used in the
lua script.
Here is my code, if you are intersted:
*ProtectedPtr.hpp*:
//------------------------------------------------------------------------------
#ifndef PROTECTED_PTR_HPP
#define PROTECTED_PTR_HPP
//------------------------------------------------------------------------------
#include "MyException.hpp"
//------------------------------------------------------------------------------
class ProtectedClassBase;
//------------------------------------------------------------------------------
struct ptrInfo
{
ProtectedClassBase *ptr;
int refCount;
};
//------------------------------------------------------------------------------
template<class T>
class ProtectedPtr
{
public:
// Constructors
ProtectedPtr( const ProtectedPtr<T>& ptr )
{
m_ptrInfo = ptr.m_ptrInfo;
m_ptrInfo->refCount++;
}
ProtectedPtr( T* ptr )
{
m_ptrInfo = ptr->GetPtrInfo();
m_ptrInfo->refCount++;
}
// Destructors
virtual ~ProtectedPtr()
{
if(m_ptrInfo)
{
if(m_ptrInfo->refCount == 1) // Only this pointer remains
{
delete m_ptrInfo;
}
else
{
m_ptrInfo->refCount--;
}
}
}
// Get
T* GetPtr() const
{
if( m_ptrInfo->ptr == NULL)
{
MY_THROW("You are trying to access a deleted Object !");
}
return static_cast<T*>(m_ptrInfo->ptr);
}
T* operator->() const
{
return GetPtr();
}
operator T*() const
{
return GetPtr();
}
// Valid
bool IsValid() const
{
return (m_ptrInfo->ptr != NULL);
}
protected:
ptrInfo *m_ptrInfo;
};
//------------------------------------------------------------------------------
template<class T>
T* get_pointer( ProtectedPtr<T> ptr )
{
return ptr.GetPtr();
}
//------------------------------------------------------------------------------
class ProtectedClassBase
{
public:
ProtectedClassBase();
ProtectedClassBase(const ProtectedClassBase &src);
virtual ~ProtectedClassBase();
ptrInfo *GetPtrInfo();
bool HaveReferencedPtr() const;
private:
ptrInfo *m_ptrInfo;
};
//------------------------------------------------------------------------------
#endif // PROTECTED_PTR_HPP
*ProtectedPtr.cpp:*
//------------------------------------------------------------------------------
#include "ProtectedPtr.hpp"
//------------------------------------------------------------------------------
ProtectedClassBase::ProtectedClassBase()
{
m_ptrInfo = new ptrInfo();
m_ptrInfo->ptr = this;
m_ptrInfo->refCount = 1;
}
//------------------------------------------------------------------------------
ProtectedClassBase::ProtectedClassBase(const ProtectedClassBase &src)
{
m_ptrInfo = new ptrInfo();
m_ptrInfo->ptr = this;
m_ptrInfo->refCount = 1;
}
//------------------------------------------------------------------------------
ProtectedClassBase::~ProtectedClassBase()
{
if(m_ptrInfo->refCount == 1) // Only the object itself left
{
delete m_ptrInfo;
}
else // Pointer on this object remains
{
m_ptrInfo->refCount--;
m_ptrInfo->ptr = NULL;
}
}
//------------------------------------------------------------------------------
ptrInfo *ProtectedClassBase::GetPtrInfo()
{
return m_ptrInfo;
}
//------------------------------------------------------------------------------
bool ProtectedClassBase::HaveReferencedPtr() const
{
return (m_ptrInfo->refCount != 1);
}
//------------------------------------------------------------------------------
2011/12/2 Nigel J Atkinson <[email protected]>
> You might be able to search through _G and any tables you find within, for
> a particular userdata, and set anything that refs it to nil, but that
> doesn't strike me as something that could be done quickly, and safely.
> That's gut instinct though, I could be wrong.
>
> Nigel
>
> On 02/12/2011, at 3:02 AM, eduard mueller wrote:
>
> > 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
>
>
>
> ------------------------------------------------------------------------------
> 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