segmentation fault while using ctypes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patelss23
    New Member
    • Jul 2007
    • 16

    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 */
    Code:
    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)){
         data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
         whereami += data_size;
         fp_more = fp_feed_short(id,data,data_size); // call to some
    library funtion
     } //end while
    
    /* some arithmetic calculations here */
    
      n = hash_calculate(id,length,hash,&f,&d);
    
      if (data != NULL)
          free(data)
      fclose(fp)
      return n;
    
    }
    /*** END OF C FUNCTION ******/
    --------------------------------------------------------------------
    Python code
    ---------------------------------------------------------------------
    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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?

    Comment

    • patelss23
      New Member
      • Jul 2007
      • 16

      #3
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by patelss23
        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.
        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?

        Comment

        Working...