title: Memory leak with SWIG
body: <p>I have a class that upon initialization, reserves an array of pointers with "new".  Upon destruction, that array of pointers is released with "delete[]".</p>
  
  <p>There's no problem when running the code <em>without</em> python.  However, when I "swig'it" and use it as a python module, something weird happens.  The destructor is called correctly upon garbage collection, but when doing that, a memory leak occurs!  A complete mystery (at least to me).  Help highly appreciated!</p>
  
  <p>(1) Preliminaries to get it compiled:</p>
  
  <p>setup.py</p>
  
  <pre><code>from setuptools import setup, Extension, find_packages
  import os
  import copy
  import sys
  
  def make_pps():
    d=[]
    c=[]
    l=[]
    lib=[]
    s=[]
    s+=["-c++"]
    return Extension("_pps",sources=["pps.i","pps.cpp"],include_dirs=d,extra_compile_args=c,extra_link_args=l,libraries=lib,swig_opts=s)
  
  ext_modules=[]
  ext_modules.append(make_pps())  
  
  setup(
    name = "pps",
    version = "0.1",
    packages = find_packages(),
    install_requires = ['docutils&gt;=0.3'],
    ext_modules=ext_modules
  )
  </code></pre>
  
  <p>pps.i</p>
  
  <pre><code>%module pps
  %{
  #define SWIG_FILE_WITH_INIT
  %}
  
  %inline %{
  
  
  class MemoryManager {
  
  public:
    MemoryManager(int n);
    ~MemoryManager();
  };
  
  %}
  </code></pre>
  
  <p>(2) The C++ code itself:</p>
  
  <p>pps.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;                             
  #include &lt;stdio.h&gt;
  #include &lt;typeinfo&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;stdint.h&gt;  
  #include &lt;cmath&gt;                       
  
  #include &lt;cstring&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include&lt;sys/ipc.h&gt; // shared memory
  #include&lt;sys/shm.h&gt;
  
  
  /*
  Stand-alone cpp program: 
    - UNComment #define USE_MAIN (see below)
    - compile with
  
      g++ pps.cpp
  
    - run with
  
      ./a.out
  
      =&gt; OK
  
    - Check memory leaks with
  
      valgrind ./a.out
  
      =&gt; ALL OK/CLEAN
  
  
  Python module:
    - Comment (i.e. use) the USE_MAIN switch (see below)
    - compile with
  
      python3 setup.py build_ext; cp build/lib.linux-x86_64-3.5/_pps.cpython-35m-x86_64-linux-gnu.so ./_pps.so
  
    - run with
  
      python3 test.py
  
      =&gt; CRASHHHHH
  
    - Check memory leaks with
  
      valgrind python3 test.py
  
      =&gt; Whoa..
  
    - Try to enable/disable lines marked with "BUG?" .. 
  
  */
  
  // #define USE_MAIN 1 // UNcomment this line to get stand-alone c-program
  
  
  using std::cout; 
  using std::endl;
  using std::string;
  
  
  class MemoryManager {
  
  public:
    MemoryManager(int n);
    ~MemoryManager();
  
  private:
    int nmax;
    int nc; // next index to be used
    uint nsum;
    int* ids;
    void** buffers;
  };
  
  
  MemoryManager::MemoryManager(int n) : nmax(n),nc(0) {
    cout &lt;&lt; "MemoryManager: nmax="&lt;&lt;this-&gt;nmax&lt;&lt;"\n";
  
    this-&gt;buffers  =new void*[this-&gt;nmax]; // BUG?
    this-&gt;ids      =new int  [this-&gt;nmax];
    this-&gt;nsum     =0;
  }
  
  
  MemoryManager::~MemoryManager() {
    printf("MemoryManager: destructor\n");
    delete[] this-&gt;buffers;               // BUG?
    delete[] this-&gt;ids;
    printf("MemoryManager: destructor: bye\n");
  }
  
  
  #ifdef USE_MAIN
  int main(int argc, char *argv[]) {
    MemoryManager* m;
  
    m=new MemoryManager(1000);
    delete m;
  
    m=new MemoryManager(1000);
    delete m;
  }
  #endif
  </code></pre>
  
  <p>(3) A test python program:</p>
  
  <p>test.py</p>
  
  <pre><code>from pps import MemoryManager
  import time
  
  print("creating MemoryManager")
  mem=MemoryManager(1000)
  time.sleep(1)
  print("clearing MemoryManager")
  mem=None
  print("creating MemoryManager (again)")
  time.sleep(1)
  mem=MemoryManager(1000)
  time.sleep(1)
  print("exit")
  </code></pre>
  
  <p>Compile with:</p>
  
  <blockquote>
    <p>python3 setup.py build_ext; cp build/lib.linux-x86_64-3.5/_pps.cpython-35m-x86_64-linux-gnu.so ./_pps.so</p>
  </blockquote>
  
  <p>Run with:</p>
  
  <blockquote>
    <p>python3 test.py</p>
  </blockquote>
  
  <p><strong>EDIT AND OFF-TOPIC</strong></p>
  
  <p>As a question of this characteristics always attracts people who think they can fix everything by using containers instead of raw pointer structures, here is the container version (might be wrong .. not used vectors much, but anyway, its off-topic):</p>
  
  <pre><code>class MemoryManager {
  
  public:
    MemoryManager(int n);
    ~MemoryManager();
  
  private:
    int nmax;
    int nc; // next index to be used
    uint nsum;
  
    // "ansi" version
    //int* ids;
    //void** buffers;
  
    // container version
    vector&lt;int&gt;   ids;
    vector&lt;void*&gt; buffers;
    // vector&lt;shared_ptr&lt;int&gt;&gt; buffers; // I feel so modern..
  };
  
  
  MemoryManager::MemoryManager(int n) : nmax(n),nc(0) {
    cout &lt;&lt; "MemoryManager: nmax="&lt;&lt;this-&gt;nmax&lt;&lt;"\n";
  
    /* // "ansi" version
    this-&gt;buffers  =new void*[this-&gt;nmax]; // BUG?
    this-&gt;ids      =new int  [this-&gt;nmax];
    */
  
    // container version
    this-&gt;ids.reserve(10);
    this-&gt;buffers.reserve(10);
  
    this-&gt;nsum     =0;
  }
  
  
  MemoryManager::~MemoryManager() {
    printf("MemoryManager: destructor\n");
  
    /* // "ansi" version
    delete[] this-&gt;buffers;               // BUG?
    delete[] this-&gt;ids;
    */
    printf("MemoryManager: destructor: bye\n");
  }
  </code></pre>
  
  <p>Doesn't work either</p>
  
  <p><strong>FINAL REMARKS</strong></p>
  
  <p>Thank you for Flexos for amazing/detailed analysis.</p>
  
  <p>My initial reason for using inconsistent class declaration was, that I routinely don't want to expose all details of my class in python.</p>
  
  <p>I forgot that in Swig interface file, everything that's put in the </p>
  
  <p><code>%{..}</code> </p>
  
  <p>is prepended into the generated wrapper code .. now that was missing, so the wrapper code got the class declaration only from the <code>%inline</code> section..! </p>
  
  <p>We can still have a minimal part of a the class wrapped, with the following pps.i file:</p>
  
  <pre><code>%module pps
  %{
  #define SWIG_FILE_WITH_INIT
  #include "pps.h"
  %}
  
  class MemoryManager {
  
  public:
    MemoryManager(int n);
    ~MemoryManager();
  };
  </code></pre>
  
  <p>Where "pps.h" should have the correct class declaration.  Now <code>#included "pps.h"</code> appears in the beginning of "pps_wrap.cpp".  </p>
  
  <p>The "class declaration" in "pps.i" only tells Swig what we want to wrap ..</p>
  
  <p>.. right..? (no more memory errors, at least)</p>