Connecting Tech Pros Worldwide Forums | Help | Site Map

segmentation fault while using ctypes

Newbie
 
Join Date: Jul 2007
Posts: 16
#1: Apr 14 '09
Hello All,

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

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 */
Expand|Select|Wrap|Line Numbers
  1. int get_hash(char *filename,int rate,int ch,unsigned char* hash,
  2. unsigned int* hash_size,short* avg_f,short* avg_d){
  3.  
  4. /* some variable declarations here */
  5. fp = fopen(filename,"rb");
  6.  
  7. data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS);
  8.  
  9. whereami = WAVE_HEADER_SIZE;
  10. while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){
  11.      data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
  12.      whereami += data_size;
  13.      fp_more = fp_feed_short(id,data,data_size); // call to some
  14. library funtion
  15.  } //end while
  16.  
  17. /* some arithmetic calculations here */
  18.  
  19.   n = hash_calculate(id,length,hash,&f,&d);
  20.  
  21.   if (data != NULL)
  22.       free(data)
  23.   fclose(fp)
  24.   return n;
  25.  
  26. }
  27.  
/*** END OF C FUNCTION ******/
--------------------------------------------------------------------
Python code
---------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. from ctypes import *
  2. lib = cdll.LoadLibrary("/usr/lib/libclient.so")
  3.  
  4. def my_func(filename,rate,ch):
  5.     hash = (c_ubyte * 424)()
  6.     hash_size = c_uint()
  7.     avg_f = c_short(0)
  8.     avg_d = c_short(0)
  9.     n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref
  10. (avg_f),byref(avg_d))
  11.     hash = None
  12.  
  13. def main():
  14.     for filename in os.listdir(MY_DIR):
  15.             print filename
  16.             my_func(filename,100,10)
  17.             print
  18. "----------------------------------------------------"
  19.  
  20. if __name__ == "__main__":
  21.     main()
============== END OF PYTHON CODE ==========================

Thank you in advance,
sanket

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#2: Apr 15 '09

re: segmentation fault while using ctypes


You have left some important bits out of your C code particularly the declarations of all the variables in

n = hash_calculate(id,length,hash,&f,&d);

Also what do the functions fp_feed_short and hash_calculate do? Where are they declared? Where are they defined?
Newbie
 
Join Date: Jul 2007
Posts: 16
#3: Apr 15 '09

re: segmentation fault while using ctypes


hi Banfa,

Yeah I have declared all variables correctly in the function body, just didn't mention here. fp_feed_short is an API call to some library which returns me 1 to stop. and hash_calculate is doing some (customized) kind hasing on a file.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#4: Apr 16 '09

re: segmentation fault while using ctypes


Quote:

Originally Posted by patelss23 View Post

Yeah I have declared all variables correctly in the function body, just didn't mention here. fp_feed_short is an API call to some library which returns me 1 to stop. and hash_calculate is doing some (customized) kind hasing on a file.

Something is going wrong and you don't know what! Therefore how do you know that everything is declared and initialised correctly given you have behaviour you can't explain?
Reply