title: ctypes/C++ segfault accessing member variables
body: <p>I am no stranger to the python ctypes module, but this is my first attempt at combining C++, C and Python all in one code. My problem seems to be very similar to <a href="https://stackoverflow.com/questions/12142766/seg-fault-when-using-ctypes-with-python-and-c?lq=1">Seg fault when using ctypes with Python and C++</a>, however I could not seem to solve the problem in the same way.</p>
  
  <p>I have a simple C++ file called <code>Header.cpp</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Foo{
     public:
        int nbits;
        Foo(int nb){nbits = nb;}
        void bar(){ std::cout &lt;&lt; nbits &lt;&lt; std::endl; }
  };
  
  extern "C" {
     Foo *Foo_new(int nbits){ return new Foo(nbits); }
     void Foo_bar(Foo *foo){ foo-&gt;bar(); }
  }
  </code></pre>
  
  <p>which I compile to a shared library using:</p>
  
  <pre><code>g++ -c Header.cpp -fPIC -o Header.o
  g++ -shared -fPIC -o libHeader.so  Header.o
  </code></pre>
  
  <p>and a simple Python wrapper called <code>test.py</code>:</p>
  
  <pre><code>import ctypes as C
  lib = C.CDLL('./libHeader.so')
  
  class Foo(object):
      def __init__(self,nbits):
          self.nbits = C.c_int(nbits)
          self.obj = lib.Foo_new(self.nbits)
  
      def bar(self):
          lib.Foo_bar(self.obj)
  
  def main():
      f = Foo(32)
      f.bar()
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>I would expect that when I call test.py, I should get the number 32 printed to screen. However, all I get is a segmentation fault. If I change the constructor to return the class instance on the stack (i.e. without the <code>new</code> call) and then pass around the object, the program performs as expected. Also, if I change the <code>bar</code> method in the <code>Foo</code> class such that it does not use the <code>nbits</code> member, the program does not seg fault. </p>
  
  <p>I have an limited understanding of C++, but the fact that I can make this function as expected in C and in C++ but not in Python is a little confusing. Any help would be greatly appreciated. </p>
  
  <p><strong>Update</strong>: Thanks to one of the comments below, the problem has been solved. In this case, an explicit declaration of both restype and argtypes for the C functions was required. i.e the following was added to the python code:</p>
  
  <pre><code>lib.Foo_new.restype  = C.c_void_p
  lib.Foo_new.argtypes = [C.c_int32]
  lib.Foo_bar.restype  = None
  lib.Foo_bar.argtypes = [C.c_void_p]
  </code></pre>