From 7c5434a99fbf2a702ccf2dbed5a8710d551f9643 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Wed, 4 Dec 2013 11:15:11 +0100 Subject: [PATCH] cppbind: don't leak nasal references for Ghost methods. --- simgear/nasal/cppbind/Ghost.hxx | 37 +++++++++++---------- simgear/nasal/cppbind/NasalObjectHolder.cxx | 2 +- simgear/nasal/cppbind/NasalObjectHolder.hxx | 5 ++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index 30524df3..b52f825e 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -21,6 +21,7 @@ #define SG_NASAL_GHOST_HXX_ #include "NasalCallContext.hxx" +#include "NasalObjectHolder.hxx" #include @@ -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, diff --git a/simgear/nasal/cppbind/NasalObjectHolder.cxx b/simgear/nasal/cppbind/NasalObjectHolder.cxx index 555a2735..e2f190b0 100644 --- a/simgear/nasal/cppbind/NasalObjectHolder.cxx +++ b/simgear/nasal/cppbind/NasalObjectHolder.cxx @@ -35,7 +35,7 @@ namespace nasal } //---------------------------------------------------------------------------- - SGSharedPtr ObjectHolder::makeShared(naRef obj) + ObjectHolderRef ObjectHolder::makeShared(naRef obj) { return new ObjectHolder(obj); } diff --git a/simgear/nasal/cppbind/NasalObjectHolder.hxx b/simgear/nasal/cppbind/NasalObjectHolder.hxx index c63b059d..47b13d52 100644 --- a/simgear/nasal/cppbind/NasalObjectHolder.hxx +++ b/simgear/nasal/cppbind/NasalObjectHolder.hxx @@ -25,6 +25,9 @@ namespace nasal { + class ObjectHolder; + typedef SGSharedPtr 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 makeShared(naRef obj); + static ObjectHolderRef makeShared(naRef obj); protected: naRef _ref;