]> git.mxchange.org Git - simgear.git/commitdiff
cppbind: don't leak nasal references for Ghost methods.
authorThomas Geymayer <tomgey@gmail.com>
Wed, 4 Dec 2013 10:15:11 +0000 (11:15 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Wed, 4 Dec 2013 10:15:11 +0000 (11:15 +0100)
simgear/nasal/cppbind/Ghost.hxx
simgear/nasal/cppbind/NasalObjectHolder.cxx
simgear/nasal/cppbind/NasalObjectHolder.hxx

index 30524df34d368447c9a7448c1d4e111dd6071fa8..b52f825ea0bc2d52e4bc57ab2559becf9b0b43dc 100644 (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,
index 555a27359d7e122bf6f6a548e1394426c8753bba..e2f190b0c032cd839ac339090045df6cdc19ccc2 100644 (file)
@@ -35,7 +35,7 @@ namespace nasal
   }
 
   //----------------------------------------------------------------------------
-  SGSharedPtr<ObjectHolder> ObjectHolder::makeShared(naRef obj)
+  ObjectHolderRef ObjectHolder::makeShared(naRef obj)
   {
     return new ObjectHolder(obj);
   }
index c63b059db0757e91246292b190c9981eed5ae2ba..47b13d52d216d46a12371270456a3092623c1986 100644 (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;