when it reaches end-of-file (stop=1) segmentation fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    when it reaches end-of-file (stop=1) segmentation fault

    When I reach end-of-file, I gets segmentation fault with below prog, I just read from a file, then print the time stamp, when reaches end-of-file, sets "stop=1", but my program dumps core, any idea?.

    TS: 18:58:57:291
    TS: 18:58:57:385
    TS: 18:58:57:494
    TS: 18:58:57:588
    TS: 18:58:57:682
    TS: 18:58:57:791

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 1077195904 (LWP 6583)]
    0x00000000 in ?? ()
    (gdb) bt
    #0 0x00000000 in ?? ()
    (gdb)


    Code:
    int main(int argc, const char * argv[])
    {
            time_stamp_t TS;
            DATA_T meta;
            int stop=0;
            int metagen_ptr;
    
            FileLoaderStart("Receiver.ini");
    
            while (1) {
                    FileLoader(&TS, &meta, &stop,  &metagen_ptr);
                    if (stop == 1){
                            return 0;
                    }
                    printf("TS: %d:%d:%d:%d\n", TS.hourofday, TS. minuteofhour, TS.secofminute, TS.millisec);
    
                    //sleep(1);
            }
    
            return 0;
    }
    
    void FileLoaderStart(char*cfgfile)
    {
       char metafilename[50];
       META_DATA_T meta;
    
            memset(metafilename,'\0',CFG_FILE_NAME_LENGTH);
            htGetParam(cfgfile,"metafilename",metafilename);
    
            fmetafile=fopen(metafilename,"r");
            if(fmetafile==NULL)return;
    
            memset(linebuffer,0,81920);
    
            if(fgets(linebuffer,MAX_LINE_SIZE,fmetafile)==NULL){
                    fclose(fmetafile);
                    fmetafile=NULL;
                    return;
            }
    
             FileParseOneLine(linebuffer,&pre_TS,&pre_meta);
    
    }
    
    
    
    void FileLoader(time_stamp_t* time_stamp, DATA_T* blob_array,int*stop,int*metagen_ptr)
    {
    
    
            time_stamp_t TS;
            DATA_T meta;
            int blob_index=0;
            DATA_T* blob_ptr=blob_array;
    
            memset(blob_array,0,sizeof(DATA_T)*MAX_BLOB_NUM);
            *stop=0;
    
    
    
    
            while(1){
                    memset(linebuffer,0,81920);
                    if(fgets(linebuffer,MAX_LINE_SIZE,fmetafile)==NULL){
                            fclose(fmetafile);
                            *stop=1;
                            return ;
                    }
    
                    MetaFileParseOneLine(linebuffer,&TS,&meta);
                    if(pre_TS.year==TS.year &&pre_TS.month==TS.month&&pre_TS.dayofmonth==TS.dayofmonth
                &&pre_TS.hourofday==TS.hourofday&& pre_TS.minuteofhour==TS.minuteofhour&& pre_TS.secofminute==TS.secofminute
                    &&pre_TS.millisec==TS.millisec&&blob_index<MAX_BLOB_NUM){
    
    
    
    }
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    #2
    I added more prints, segmentation fault coming from main function only, when I check stop == 1 then return, that return causing this violation, but when I reaches EOF, I want to exit the prog.

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      GDB shows line 61 fgets, any command to view what causes violation?

      TS: 18:58:57:791
      ***EOF ****

      Program received signal SIGSEGV, Segmentation fault.
      [Switching to Thread 1077195904 (LWP 9254)]
      0x402835aa in fgets () from /lib/tls/libc.so.6
      (gdb) bt
      #0 0x402835aa in fgets () from /lib/tls/libc.so.6
      #

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Ot ook like you are passing the address of:

        Code:
        DATA_T meta;
        to your FileLoader function as the address of an array of DATA_T, which it's not.

        Comment

        • tvnaidu
          Contributor
          • Oct 2009
          • 365

          #5
          Thanks, I found it. now it works.

          Comment

          Working...