Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: ctypes + C malloc error. C memory problem or Python/ctypes problem?
    primarykey
    data
    text
    <p>everyone. I'm having a memory allocation error using ctypes and a C code. I'm wondering if the memory problem is inside of C, or caused by an improper use of ctypes. The memory error is</p> <blockquote> <p>python(79698) malloc: <strong>* error for object 0x15627ac08: incorrect checksum for freed object >- object was probably modified after being freed. *</strong> set a breakpoint in malloc_error_break to debug Abort</p> </blockquote> <p>or</p> <blockquote> <p>python(76110,0x7fff70062ca0) malloc: <strong>* error for object 0x7d807e1078907df: pointer being >freed was not allocated *</strong> set a breakpoint in malloc_error_break to debug Abort</p> </blockquote> <p>depending on how I write the "free_some_memory" part of the code below. I won't post a bunch of C code here, but assuming I've written the C code correctly, <em>does my ctypes interface look correct?</em> </p> <p>Also, the failures I'm experiencing don't always happen in the same place in my scripts, for a given version of "free_some_memory". <em>Is it possible that my C code memory management is written poorly such that Python memory management may or may not bungle up (de)allocations?</em> </p> <p>Dunno if this is important, but I'm working on a Mac, Snow Leopard.<br> Any help you can offer would be very appreciated.</p> <p>Best, jkmacc</p> <p>My C code looks like:</p> <pre><code>/**** cfunction.c ****/ int cfun (double *y, char datafile[1024], int byteoffset, int num ) { allocate_some_memory; do_stuff; if ( error_condition ) { free_some_memory; return ( -1 ); } fill_y_here; free_some_memory; return ( 0 ); } </code></pre> <p>My ctypes wrapper looks like:</p> <pre><code>#pyfunction.py import ctypes as C import numpy as np lib = C.CDLL('mylib.dylib') def pyfun(DATAFILE, BYTEOFFSET, NUM): """ DATAFILE: a string file name BYTEOFFSET: an integer NUM: an integer """ #return integer success/failure flag lib.cfun.restype = C.c_int #array memory to be filled inside of C Y = np.empty(NUM,dtype='double',order='C') cDATAFILE = C.create_string_buffer(DATAFILE,1024) cBYTEOFFSET = C.c_int(int(BYTEOFFSET)) cNUM = C.c_int(int(NUM)) flag = lib.cfun(Y.ctypes.data_as(C.POINTER(C.c_double)), \ cDATAFILE,cBYTEOFFSET,cNUM) if flag == -1: print("Something went wrong.") return -1 else: return Y </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload