By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

segmentation fault while using ctypes

P: 16
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
Apr 14 '09 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
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?
Apr 15 '09 #2

P: 16
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.
Apr 15 '09 #3

Banfa
Expert Mod 5K+
P: 8,916
@patelss23
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?
Apr 16 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.