【问题标题】:Segmentation fault after writing some data into a file将一些数据写入文件后出现分段错误
【发布时间】:2013-09-02 12:16:08
【问题描述】:

我正在编写一个 C 程序,它计算 94000 个图像区域的度量并将值写入文件中。我可以成功编写 51069 个区域的指标,但之后 C 程序输出了一个分段错误。代码很简单:

int main(int argc, char** argv)
{

  Image *cimg=NULL;
  Image *mask=NULL;
  Image *gt=NULL;
  Image *rmask=NULL;
  Image *nwcimg=NULL;
  Image *nwrmask=NULL;
  Histogram *hist=NULL;
  FILE* output; 

  int x, r, j, y, maxR, minR, index, hsize, value, relevance;
  int min_x, min_y, max_x, max_y;

  //just checking the parameters
  if (argc != 5) 
  {
      fprintf(stderr,"usage: generatemetric<image> <region mask> <ground truth> <feature_file> \n");
      exit(-1);
  }  


    //reading the input image 
    cimg = ReadImage(argv[1]);

    //reading the image segmentation
    mask = ReadImage(argv[2]);

    //reading the ground truth which i will use to label the image regions as +1 and -1
    gt = ReadImage(argv[3]);

    //maximum and minimum region value
    maxR = MaximumValue(mask);
    minR = MinimumValue(mask);

    //i will iterate through all the image regions
    for(r=minR; r <= maxR; r++) 
    {  

            //this is done because I want to know 2 points in the image region
            //to create a bounding box on it
            min_x = mask->ncols-1;
            min_y = mask->nrows-1;
            max_x = 0;
            max_y = 0;


            for(y=0; y < mask->nrows; y++) 
            {
                 for(x=0; x < mask->ncols;x++)
                 {  
                    index = y*(mask->ncols)+x;

                    if(mask->val[index] == r) 
                    {
                          if(x < min_x)
                          min_x = x;
                          if(y < min_y)
                          min_y = y;
                          if(x > max_x)
                          max_x = x;
                          if(y > max_y)
                          max_y = y;
                     }
                   }
                 }

        nwcimg = CreateImage(max_x-min_x,max_y-min_y);
        nwrmask = CreateImage(max_x-min_x,max_y-min_y);

        //creating ROIS
        CreateROI(cimg, nwcimg, min_x, min_y, max_x, max_y);
        CreateROI(mask, nwrmask, min_x, min_y, max_x, max_y);                
        //calculating region class
        relevance = isRelevant(gt, mask, r);

        //makes the region of interest white in the bounding box
        make_binary(nwrmask,r);

      //calculates the metric only in the bounding box around the region of interest (a vector that is a kind of histogram)
        hist=metric(nwcimg,nwrmask);

       //starting to write

       //histogram size
       hsize = hist->n;

      //open the file to append the values
      //the problem starts here after 51069 iterations
      output = fopen(argv[4], "a+");

      //record the region class in the file         
      fprintf(output, "%d, ", relevance);

      //record each value of the histogram as comma separated values
      for(j = 0; j < hsize; j++) 
      {
            value = hist->v[j];
            fprintf(output, "%d, ", value);
      }
      //recod the region id
      fprintf(output,"%d \n", r+1);
      fclose(output);
  DestroyHistogram(&hist);
  DestroyImage(&nwcimg);
  DestroyImage(&nwrmask);

}
DestroyImage(&rmask);
DestroyImage(&gt);
DestroyImage(&cimg);
DestroyImage(&mask);
return(0);
}

换句话说,在每次 94000 次迭代中,程序都会打开文件,写入值并关闭它。我想也许程序运行时我有很多 I/O。我对吗?还是其他类型的问题?这里有人遇到过这个问题吗?如何解决?

【问题讨论】:

  • 在调试器中运行程序,这样你就会被告知在哪里程序崩溃了。
  • 听起来你正在泄漏内存,可能是在metric 函数中。向我们展示此代码和/或在其上运行调试器。
  • 您的metric 函数可能存在错误,导致它在分配的内存之外写入,破坏堆并导致诸如mallocfopen 之类的函数发生故障。还不清楚为什么在循环的每次迭代中都以附加模式调用fopen - 你不能这样做一次吗?
  • @mad 并不能解决问题(但它可能会隐藏它,这可以说更糟),重复fopen 的效率低下只是一个旁白。
  • 好吧,显然有些事情是错误的。您需要提供足够的源代码来制作一个可以编译的独立示例(包含编译所需的所有源代码,具有main 和相关的输入/输出数据)[注意这并不意味着您应该发布> 1000 行代码供我们调试您的问题]。或者你将不得不调试你的代码。您发布的代码没有明显错误,但在某些其他代码中存在很多错误的可能性。

标签: c file io printf


【解决方案1】:

按照用户 4815162342 的建议,将文件从循环中打开并在循环后关闭。我认为问题在于大量的 i/o,而且我大学的集群中有一些并发进程。该程序立即运行完毕。谢谢大家的关注和帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2018-08-06
    • 2014-04-04
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多