segmentation fault while using ctypes

sanket sanket.s.patel at
Wed Apr 15 00:39:58 CEST 2009

Hello All,

I am dealing with this weird bug.
I have a function in C and I have written python bindings for it using

I can call this function for couple of times and then suddenly it
gives me seg fault.
But I can call same function from a C code for any number of times.

I cannot get what's going on.

here is my code.

/* C Function I am calling */
int get_hash(char *filename,int rate,int ch,unsigned char* hash,
unsigned int* hash_size,short* avg_f,short* avg_d){

/* some variable declarations here */
fp = fopen(filename,"rb");

data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS);

whereami = WAVE_HEADER_SIZE;
while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){
     fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
     whereami += fp_data_size;
     fp_more = fp_feed_short(fooid,data,fp_data_size); // call to some
library funtion
 } //end while

/* some arithmetic calculations here */

  n = my_fp_calculate(fooid,audio_length,fp_fingerprint,&fit,&dom);

  if (data != NULL)
  return n;

/************************* END OF C FUNCTION
Python code
from ctypes import *
lib = cdll.LoadLibrary("/usr/lib/")

def my_func(filename,rate,ch):
    hash = (c_ubyte * 424)()
    hash_size = c_uint()
    avg_f = c_short(0)
    avg_d = c_short(0)
    n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref
    hash = None

def main():
    for filename in os.listdir(MY_DIR):
            print filename

if __name__ == "__main__":

============== END OF PYTHON CODE ==========================

Thank you in advance,

More information about the Python-list mailing list