title: Exposing STL structs in Python w/o memory leak
body: <p>I have a <code>std::vector&lt;CameraT&gt;</code> which I've already bound to Python thanks to Flexo's response for <a href="https://stackoverflow.com/questions/13587791/swig-and-c-memory-leak-with-vector-of-pointers/13593596">SWIG and C++ memory leak with vector of pointers</a>. But I need to access fields like CameraT.t and I can't find a proper way to do it. </p>
  
  <p>It is defined in a 3rd party header file (DataInterface.h) as:</p>
  
  <pre><code>template&lt;class FT&gt;
  struct CameraT_ {
      typedef FT float_t;
      float_t t[3];
      /* more elaborate c++ stuff */
  };
  typedef CameraT_&lt;float&gt; CameraT;
  </code></pre>
  
  <p>The interface file looks roughly like this:</p>
  
  <pre><code>%module nvm
  %{
      #define SWIG_FILE_WITH_INIT
      #include "util.h"
  %}
  
  %include "std_vector.i"
  
  %inline %{
  bool CameraDataFromNVM(const char* name, std::vector&lt;CameraT&gt;&amp; camera_data){
      /* wrapper code */
  }
  %}
  
  %template(CamVector) std::vector&lt;CameraT&gt;;
  </code></pre>
  
  <p>And the integration test:</p>
  
  <pre><code>import nvm
  
  if __name__ == "__main__":
      datafile = '../../example-data/vidstills.nvm'
      cams = nvm.CamVector()
  
      assert nvm.CameraDataFromNVM(datafile, cams)
  
      print(cams[0])  # this yields no memory warnings
      print(cams[0].t[0])  # 'SwigPyObject' object has no attribute 't'
  
      # Yields: swig/python detected a memory leak of type 'CameraT *', no destructor found.
      c = list([i for i in cams])
  </code></pre>
  
  <p>prints this:</p>
  
  <pre><code>$ python3 test_nvm.py |&amp; head
  &lt;Swig Object of type 'CameraT *' at 0x7f638228ee40&gt;
  Loading cameras/points: ../../example-data/vidstills.nvm
  329 cameras; 8714 3D points; 74316 projections
  Traceback (most recent call last):
    File "test_nvm.py", line 20, in &lt;module&gt;
      print(cams[0].t[0])
  AttributeError: 'SwigPyObject' object has no attribute 't'
  swig/python detected a memory leak of type 'CameraT *', no destructor found.
  </code></pre>
  
  <p>I've tried to place <code>struct CameraT {}</code> and <code>#include</code> statements in and outside the <em>%inline</em> block and failed. Also looping over the vector elements yields the memory leak warning. No idea what else to do.</p>
  
  <p>The code is on <a href="https://github.com/lusitania/interfacing-python/tree/master/src/swig" rel="nofollow">Github</a>.</p>