segmentation fault while using ctypes

sanket sanket.s.patel at gmail.com
Wed Apr 15 13:53:52 EDT 2009


On Apr 14, 9:00 pm, bieff... at gmail.com wrote:
> On Apr 15, 12:39 am, sanket <sanket.s.pa... at gmail.com> 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");
>
> > data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS);
>
> > whereami = WAVE_HEADER_SIZE;
> > while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){
> >      fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
> >      whereami += fp_data_size;
> >      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)
> >   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
>
> You don't show how the C function make use of parameters hash,
> hash_size, avg_f, avg_d. Since you pass them by address,
> I guess that are output parameters, but how do they get written? In
> particular,make sure you don't write into hash more
> than the  424 bytes you allocated for it in the calling python
> code ...
>
> Ciao
> ----
> FB

Yes, I am making sure that hash wouldn't be written more that 424
bytes in to it.
This is something really weird. I can call this function from C as
many times as I want but not from python

I tracked down the problem and came to know that call to
my_fp_calculate causes the seg fault.
I will debug it and let you guys know.

Thank you all so much for the help. I appreciate it.
sanket



More information about the Python-list mailing list