title: Freeing memory when using ctypes
body: <p>I am using ctypes to try and speed up my code.</p>
  
  <p>My problem is similar to the one in this tutorial : <a href="https://cvstuff.wordpress.com/2014/11/27/wraping-c-code-with-python-ctypes-memory-and-pointers/" rel="nofollow noreferrer">https://cvstuff.wordpress.com/2014/11/27/wraping-c-code-with-python-ctypes-memory-and-pointers/</a></p>
  
  <p>As pointed out in the tutorial I should free the memory after using the C function. Here is my C code</p>
  
  <pre><code>//C functions
  double* getStuff(double *R_list, int items){
  
      double results[items];  
      double* results_p;   
  
      for(int i = 0; i &lt; items; i++){ 
              res = calculation ; \\do some calculation
              results[i] = res;   }
  
          results_p = results;
          printf("C allocated address %p \n", results_p);
  
  return results_p; }
  
  void free_mem(double *a){
      printf("freeing address: %p\n", a);
  
      free(a);    }
  </code></pre>
  
  <p>Which I compile with <code>gcc -shared -Wl,-lgsl,-soname, simps -o libsimps.so -fPIC simps.c</code></p>
  
  <p>And python: </p>
  
  <pre><code>//Python
  from ctypes import *
  import numpy as np
  
  
  mydll = CDLL("libsimps.so")
  mydll.getStuff.restype = POINTER(c_double)
  mydll.getStuff.argtypes = [POINTER(c_double),c_int]
  mydll.free_mem.restype = None
  mydll.free_mem.argtypes = [POINTER(c_double)]
  
  R = np.logspace(np.log10(0.011),1, 100, dtype = float) #input
  
  
  tracers = c_int(len(R))
  
  R_c = R.ctypes.data_as(POINTER(c_double))
  
  
  for_list = mydll.getStuff(R_c,tracers)
  print 'Python allocated', hex(for_list)
  for_list_py = np.array(np.fromiter(for_list, dtype=np.float64, count=len(R)))
  mydll.free_mem(for_list)
  </code></pre>
  
  <p>Up to the last line the code does what I want it to and the <code>for_list_py</code> values are correct. However, when I try to free the memory, I get a Segmentation fault <s>and on closer inspection the address associated with <code>for_list</code> --> <code>hex(for_list)</code> is different to the one allocated to <code>results_p</code> within C part of the code. </s></p>
  
  <p><s>As pointed out in this question, <a href="https://stackoverflow.com/questions/13445568/python-ctypes-how-to-free-memory-getting-invalid-pointer-error">Python ctypes: how to free memory? Getting invalid pointer error</a>    , <code>for_list</code> will return the same address if <code>mydll.getStuff.restype</code> is set to <code>c_void_p</code>. But then I struggle to put the actual values I want into <code>for_list_py</code>. This is what I've tried:</s>
  <s>
      cast(for_list, POINTER(c_double) )
      for_list_py = np.array(np.fromiter(for_list, dtype=np.float64, count=len(R)))
      mydll.free_mem(for_list)
  </s>
  <s>where the cast operation seems to change for_list into an integer.</s> I'm fairly new to C and very confused. Do I need to free that chunk of memory? If so, how do I do that whilst also keeping the output in a numpy array?  Thanks!</p>
  
  <p>Edit: It appears that the address allocated in C and the one I'm trying to free are the same, though I still recieve a Segmentation fault. </p>
  
  <pre><code>C allocated address 0x7ffe559a3960
  freeing address: 0x7ffe559a3960
  Segmentation fault
  </code></pre>
  
  <p>If I do <code>print for_list</code> I get <code>&lt;__main__.LP_c_double object at 0x7fe2fc93ab00&gt;</code></p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>Just to let everyone know, I've struggled with c_types for a bit. </p>
  
  <p>I've ended up opting for SWIG instead of c_types. I've found that the code runs faster on the whole (compared to the version presented here). I found this documentation on dealing with memory deallocation in SWIG very useful <a href="https://scipy-cookbook.readthedocs.io/items/SWIG_Memory_Deallocation.html" rel="nofollow noreferrer">https://scipy-cookbook.readthedocs.io/items/SWIG_Memory_Deallocation.html</a>  as well as the fact that SWIG gives you a very easy way of dealing with numpy n-dimensional arrays.</p>