segmentation fault while using ctypes
MRAB
google at mrabarnett.plus.com
Tue Apr 14 19:19:16 EDT 2009
sanket wrote:
> On Apr 14, 4:00 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
>> sanket wrote:
>>> 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 */
>>> 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");
>> You should check the value of 'fp' here.
>>
>>> data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS);
>> You should check the value of 'data' here.
>>
>>> whereami = WAVE_HEADER_SIZE;
>> What is 'whereami'?
>>
>>> while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){
>>> fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
>> 'fp_data_size' will be the number of signed shorts read, not the number
>> of bytes. Is this OK?
>>
>>> whereami += fp_data_size;
>> The final value of 'whereami' will be WAVE_HEADER_SIZE + the total
>> number of signed shorts read.
>>
>>> 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)
>>> free(data)
>> I don't that 'free()' will complain if 'data' happens to be NULL,
>> although you should've already checked whether 'data' is NULL when you
>> malloc'ed! :-)
>>
>>> fclose(fp)
>>> return n;
>>> }
>>> /************************* END OF C FUNCTION
>>> *********************************/
>>> --------------------------------------------------------------------
>>> Python code
>>> ---------------------------------------------------------------------
>>> from ctypes import *
>>> lib = cdll.LoadLibrary("/usr/lib/libclient.so")
>>> 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
>>> (avg_f),byref(avg_d))
>>> hash = None
>>> def main():
>>> for filename in os.listdir(MY_DIR):
>>> print filename
>>> my_func(filename,100,10)
>>> print
>>> "----------------------------------------------------"
>>> if __name__ == "__main__":
>>> main()
>>> ============== END OF PYTHON CODE ==========================
>>> Thank you in advance,
>>> sanket
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>
>
> Thank you for your reply.
>
> I will make check for fp and data pointers.
> But my point is this function runs fine while calling it from a C
> code.
> it only breaks while calling from python. So I wonder if there can be
> anything wrong with ctypes module.
>
It's more likely that it's a bug in your code.
More information about the Python-list
mailing list