469,602 Members | 1,998 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

segmentation fault while using ctypes

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
3 4208
Banfa
9,064 Expert Mod 8TB
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
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
9,064 Expert Mod 8TB
@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.

Similar topics

2 posts views Thread by sivignon | last post: by
5 posts views Thread by Fra-it | last post: by
18 posts views Thread by Digital Puer | last post: by
27 posts views Thread by Paminu | last post: by
7 posts views Thread by pycraze | last post: by
3 posts views Thread by madunix | last post: by
1 post views Thread by Marcus.CM | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.