gerwindehaan / osgswig

Python bindings for OpenSceneGraph (auto-export from code.google.com/p/osgswig)
Other
1 stars 1 forks source link

Fix for osg::Referenced reference counting problems #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Until shortly I always ignored the memory leaks reported by SWIG.

In order to have good reference counting, SWIG should make a default
wrapper for a deconstructor.                                   
It will not in many cases in OSG, because the deconstructors are often
protected, and so ignored by SWIG .                           
As a result, the unref SWIG feature does not work, and memory leaks are
reported during executing,                                  
 because C++ objects keep dangling in memory with references to
non-existing Python SWIG objects.                                   
Our main target is osg::Referenced, used as a base class throughout OSG   

I found a bug issued on SWIG sourceforge (most likely by another OSG
wrapper,
http://sourceforge.net/tracker/index.php?func=detail&aid=1513333&group_id=1645&a
tid=101645),
but no general solution was provided.

The only reasonable fix I could find, is to replace the normal ~Referenced
destructor for this ifdef construction in its header file:

 .... in Referenced header file ...                                       

ifdef SWIG                                                                

     public:            //20080410GDH trick SWIG into making a default wrapper for
deconstructor                               
else                                                                      

     protected:                                                           

endif                                                                     

        virtual ~Referenced();                                              

   protected:                                                             

        ... other stuff here...                                             

In this way, SWIG is made to think a wrapper for a destructor is needed.  

Combine with the ref and unref SWIG features (as used already in osg.i) to
have Python's reference counter trigger the OSG references.               

%feature("ref") osg::Referenced "$this->ref();"                           

%feature("unref") osg::Referenced "$this->unref();"                       

Use some extra printf's to track if refs and unrefs are working for your
class:                                                                    

%feature("ref") osg::Referenced "printf(\"osgRef\\n\");$this->ref();"     

%feature("unref") osg::Referenced "printf(\"osgRefUn\\n\");$this->unref();"

See the attached file for some testing.
This should not give you any warnings.

Original issue reported on code.google.com by gerwinde...@gmail.com on 11 Apr 2008 at 9:02

Attachments:

GoogleCodeExporter commented 9 years ago
Tried a different approach, but no success yet on getting SWIG to swallow this:

//attempt at a typemap to avoid "normal" pointers getting into target language 
scope
//Instead, try to return a new ref_ptr object to the original Referenced object
//see SWIG manual, Pointer Handling: 
http://www.swig.org/Doc1.3/Python.html#Python_nn64

%typemap(out) osg::Node * {
   printf("Detected Node* using typemap(out)\n");
   //Create a new , SWIG wrapped ref_ptr object and return this
   //using the function: SWIG_NewPointerObj(void *ptr, swig_type_info *ty, int own)
   //use the $descriptor() macro to get the right SWIG type
(SWIGTYPE_p_osg__ref_ptrTosg__Node_t)
   $result = SWIG_NewPointerObj($1,$descriptor(NodeRef *),1);

Original comment by gerwinde...@gmail.com on 18 Jul 2008 at 12:59

GoogleCodeExporter commented 9 years ago
I consider this pretty much solved in solved in r162.

Instead of replacing " ~Referenced " in osg/Referenced header file, it is 
extended in
SWIG.

Original comment by gerwinde...@gmail.com on 3 Aug 2008 at 11:28

GoogleCodeExporter commented 9 years ago
(status set to fixed, please let us know if you feel this is not the case)

Original comment by gerwinde...@gmail.com on 20 Jan 2009 at 8:48