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

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

问题描述

我正在解析一个制表符分隔的文本文件.它的第一列包含格式为 chrX 的字符串,其中 X 表示一组字符串,例如1"、2"、...、X",是".

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".

当文件被解析时,这些每个都存储在一个名为 chromosomechar* 中.

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

文本文件按字典顺序在第一列排序,即我将有许多行以chr1"开头,然后是chr2"等.

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.

在每个chrX"条目处,我需要打开另一个与该条目关联的文件:

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

我有函数openSourceFile,定义如下:

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
", filename);                                                                                                  
        exit(EXIT_FAILURE);                                                                                                                                                         
    }                                                                                                                                                                             
}      

问题是我的应用程序在以下行退出时出现从第一个染色体到第二个染色体(从 chr1chr2)的分段错误,我在此处关闭我打开的第一个染色体文件:

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);

我知道我没有传递 fclose 一个 NULL 指针,因为直到出现分段错误,我才从这个文件中读取数据.我什至可以将它包装在一个条件中,但我仍然得到错误:

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);
}

此外,我知道 openSourceFile 有效(至少对于 chr1,在设置 FILE* 的第一个文件句柄时),因为我的应用程序正确解析 chr1 行并从 FILE* 源文件中读取数据.

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.

导致发生分段错误的 fclose 调用的原因是什么?

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

段错误很可能是由堆上的内存损坏引起的,而不是任何影响本地人的因素.Valgrind 会立即向您显示您所做的第一个错误访问.

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.

valgrind--db-attach 选项自 2014 年 3.10.0 版以来已被弃用.发行说明指出:

The --db-attach option to valgrind has been deprecated since release 3.10.0 in 2014. The release notes state:

The built-in GDB server capabilities are superior and should be used
instead. Learn more here:

http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver

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

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