473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4504
Banfa
9,065 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,065 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

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
1
by: Marcus.CM | last post by:
Hi, I use the following ctype to load a .so library in Linux. vr = ctypes.CDLL(sstr) And the following to release it so that i can reload the library without quiting the python script. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.