FCLOSE()导致分段错误 [英] fclose() causing segmentation fault

查看:227
本文介绍了FCLOSE()导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我解析制表符分隔的文本文件。它的第一列包含格式 chrX ,其中 X 表示一组字符串,例如,1的字符串, 2,......,X,Y。

这是每一个存储在的char * 名为染色体,因为文件被解析。

该文本文件的第一列排序字典顺序,即我将拥有一个数字开头为CHR1行,然后在CHR2等。

在每个chrX条目,我需要打开与此项目相关的另一个文件:

  FILE * merbaseIn;//通过环行...如果(染色体== NULL)
    openSourceFile(安培;染色体,fieldArray [I],&放大器; merbaseIn,GENPATHIN);
其他{
    如果(的strcmp(染色体,fieldArray [I])!= 0){//新的染色体
        FCLOSE(merbaseIn); //关闭老染色体FILE PTR
        免费(染色体); //自由老染色体PTR
        openSourceFile(安培;染色体,fieldArray [I],&放大器; merbaseIn,GENPATHIN); //建立新的染色体FILE PTR
    }
}
//解析排

我具备的功能 openSourceFile 的定义如下:

 无效openSourceFile(字符**铬,为const char *领域,文件** filePtr,为const char *路径){
    字符文件名[100];
    *铬=(字符*)malloc的((为size_t)的strlen(场));
    如果(*铬== NULL){
        fprintf中(标准错误,错误:无法为染色体名分配内存!);
        出口(EXIT_FAILURE);
    }    的strcpy(*铬,场);
    sprintf的(文件名,%s%s.fa,道路,场);    * filePtr = FOPEN(文件名,R);
    如果(* filePtr == NULL){
        fprintf中(标准错误,错误:无法打开FASTA源文件%s \\ n,文件名);
        出口(EXIT_FAILURE);
    }
}

问题是,我的应用程序分段错误而从第一条染色体将第二(从 CHR1 CHR2

  FCLOSE(merbaseIn);

我知道我不是过客 FCLOSE NULL指针,因为直到分段错误,我从这个文件中读取数据。我甚至可以换这一个条件,我仍然得到错误:

 如果(merbaseIn!= NULL){
    FCLOSE(merbaseIn);
}

此外,我知道 openSourceFile 作品(至少 CHR1 ,建立的第一个文件句柄时 FILE * ),因为我的应用程序解析 CHR1 行,并读取从 FILE * 源文件正确。

这是什么这个 FCLOSE 调用导致分段错误发生?


解决方案

 的valgrind --db-附加= YES --leak检查= YES --tool = MEMCHECK --num-呼叫者= 16 --leak高分辨率=高./yourprogram ARGS

这是非常有可能的段错误是由堆,没有任何的影响当地人的内存损坏引起的。 Valgrind的会立即告诉你第一个错误的访问你做。

I have a tab-delimited text file that I am parsing. Its first column contains strings of the format chrX, where X denotes a set of strings, e.g., "1", "2", ..., "X", "Y".

These are each stored in a char* called chromosome, as the file is parsed.

The text file is sorted on the first column lexicographically, i.e., I will have a number of rows starting with "chr1", and then "chr2", etc.

At each "chrX" entry, I need to open another file that is associated with this entry:

FILE *merbaseIn;

// loop through rows...

if (chromosome == NULL)                                                                                                                                                   
    openSourceFile(&chromosome, fieldArray[i], &merbaseIn, GENPATHIN);                                                                                                      
else {                                                                                                                                                                    
    if (strcmp(chromosome, fieldArray[i]) != 0) { // new chromosome                                                                                                   
        fclose(merbaseIn); // close old chromosome FILE ptr                                                                                                                                                                                                                                    
        free(chromosome); // free old chromosome ptr                                                                                                                          
        openSourceFile(&chromosome, fieldArray[i], &merbaseIn, GENPATHIN); // set up new chromosome FILE ptr                                                                  
    }                                                                                                                                                                       
}  
// parse row

I have the function openSourceFile that is defined as follows:

void openSourceFile (char** chrome, const char* field, FILE** filePtr, const char *path) {
    char filename[100];                                                                                                                                                           
    *chrome = (char *) malloc ((size_t) strlen(field));
    if (*chrome == NULL) {                                                                                                                                                        
        fprintf(stderr, "ERROR: Cannot allocate memory for chromosome name!");                                                                                                      
        exit(EXIT_FAILURE);                                                                                                                                                         
    }                                                                                                                                                                             

    strcpy(*chrome, field);                                                                                                                                                       
    sprintf(filename,"%s%s.fa", path, field);                                                                                                                                     

    *filePtr = fopen(filename, "r");                                                                                                                                              
    if (*filePtr == NULL) {                                                                                                                                                       
        fprintf(stderr, "ERROR: Could not open fasta source file %s\n", filename);                                                                                                  
        exit(EXIT_FAILURE);                                                                                                                                                         
    }                                                                                                                                                                             
}

The problem is that my application quits with a Segmentation Fault going from the first chromosome to the second (from chr1 to chr2) at the following line, where I close the first chromosome file that I opened:

fclose(merbaseIn);

I know I'm not passing fclose a NULL pointer, because up until the Segmentation Fault, I am reading data from this file. I can even wrap this in a conditional and I still get the Fault:

if (merbaseIn != NULL) {
    fclose(merbaseIn);
}

Further, I know openSourceFile works (at least for chr1, when setting up the first file handle of FILE*) because my application parses chr1 rows and reads data from the FILE* source file correctly.

What is it about this fclose call that is causing a Segmentation Fault to occur?

解决方案

valgrind --db-attach=yes --leak-check=yes --tool=memcheck --num-callers=16 --leak-resolution=high ./yourprogram args

It's very likely the segfault is caused by memory corruption on the heap, not anything that's affecting locals. Valgrind will immediately show you the first wrong access you make.

这篇关于FCLOSE()导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆