cppbind: don't leak nasal references for Ghost methods.

This commit is contained in:
Thomas Geymayer 2013-12-04 11:15:11 +01:00
parent 26d9256ee9
commit 7c5434a99f
3 changed files with 24 additions and 20 deletions

View File

@ -21,6 +21,7 @@
#define SG_NASAL_GHOST_HXX_
#include "NasalCallContext.hxx"
#include "NasalObjectHolder.hxx"
#include <simgear/debug/logstream.hxx>
@ -124,7 +125,18 @@ namespace nasal
{
public:
virtual ~MethodHolder() {}
virtual naRef get_naRef(naContext c) = 0;
naRef get_naRef(naContext c)
{
if( !_obj )
_obj = ObjectHolder::makeShared(createNasalObject(c));
return _obj->get_naRef();
}
protected:
ObjectHolderRef _obj;
virtual naRef createNasalObject(naContext c) = 0;
};
BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
@ -202,28 +214,17 @@ namespace nasal
public internal::MethodHolder
{
public:
MethodHolder():
_naRef(naNil())
{}
explicit MethodHolder(const method_t& method):
_method(method),
_naRef(naNil())
_method(method)
{}
virtual naRef get_naRef(naContext c)
{
if( naIsNil(_naRef) )
{
_naRef = naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
naSave(c, _naRef);
}
return _naRef;
}
protected:
method_t _method;
naRef _naRef;
virtual naRef createNasalObject(naContext c)
{
return naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
}
static naRef call( naContext c,
naRef me,

View File

@ -35,7 +35,7 @@ namespace nasal
}
//----------------------------------------------------------------------------
SGSharedPtr<ObjectHolder> ObjectHolder::makeShared(naRef obj)
ObjectHolderRef ObjectHolder::makeShared(naRef obj)
{
return new ObjectHolder(obj);
}

View File

@ -25,6 +25,9 @@
namespace nasal
{
class ObjectHolder;
typedef SGSharedPtr<ObjectHolder> ObjectHolderRef;
/**
* Prevent a Nasal object from being destroyed by the garbage collector during
* the lifetime of this object.
@ -49,7 +52,7 @@ namespace nasal
*
* @param obj Object to save
*/
static SGSharedPtr<ObjectHolder> makeShared(naRef obj);
static ObjectHolderRef makeShared(naRef obj);
protected:
naRef _ref;