【问题标题】:segmentation fault when compling c++ code编译 C++ 代码时出现分段错误
【发布时间】:2020-07-04 15:10:32
【问题描述】:

我现在在我的代码中为这个问题苦苦挣扎,代码可以成功编译,但是当我运行二进制文件时,会发生分段错误,问题如下:

Program received signal SIGSEGV, Segmentation fault. _int_malloc (av=av@entry=0x7ffff6adfb20 <main_arena>, bytes=bytes@entry=15859713) at malloc.c:3802     malloc.c: No such file or directory.

Env:ubuntu 16.04 VM 工作站 Com:g++,版本:5.4.0 c++:c++11 库:imebra 5.0.1 这是我的代码:

#include <imebra/imebra.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>

#define img_height 2816
#define img_width  2816
#define img_bit    2
#define img_size img_height*img_width*img_bit //15.1MB
using namespace std;

//MONOCHROME1: indicates that the greyscale ranges from bright to dark with ascending pixel values
//MONOCHROME2: indicates that the greyscale ranges from dark to bright with ascending pixel values
/*
create an Image object
fill the image object with raw data
create a DICOM dataset
add the image to the DICOM dataset
fill all the necessary DICOM tags (e.g. sop class, instance, patient name, etc)
save the DICOM dataset
*/

int main()
{
    //ifstream mydata("/home/lixingyu/GH1.raw",ios::binary);
    //uint32_t *pImgData = (uint32_t *)malloc(img_size*sizeof(uint32_t));
    //mydata.read(pImgData,img_size);   

    FILE *fp = NULL;
    fp = fopen("/home/lixingyu/123.raw","rb");
    uint32_t *pImgData = new (std::nothrow) uint32_t (img_size);
    fread(pImgData,sizeof(uint32_t),img_size,fp);
    cout<<"success"<<endl;
/*---------program stop here -------*/

    // Creat an image 500 pixels wide , 400 pixels height
    // each sample is a 16 bit unsigned value, the colorspace
    // is monochrome_2, the higher bit used is 15
    // imebra ::MutableImage image(500,400,imebra::bitDepth_t::depthU16,"MONOCHROME_2",15);

    imebra ::MutableImage image(img_height,img_width,imebra::bitDepth_t::depthU16,"MONOCHROME2",15);

        // 1. Fill the image with data
        // We use a writing data handler to write into the image.
        // The data is committed into the image only when the writing
        // data handler goes out of scope.

        imebra::WritingDataHandlerNumeric writeIntoImage(image.getWritingDataHandler());

        for (size_t y=0;y!=img_width;++y)
        {
            for (size_t x=0; x!= img_height; ++x)
            {
                writeIntoImage.setUnsignedLong(y*img_height+x,pImgData[y*img_height+x]);
            }
        }
    // specify the tansfer syntax and the charset

    imebra::charsetsList_t charsets;

    charsets.push_back("ISO 2022 IR 6");

    //Explicit VR little endian

    imebra::MutableDataSet dataSet("1.2.840.10008.1.2.1",charsets);

    // add the image to the dataSet
    dataSet.setImage(0,image,imebra::imageQuality_t::veryHigh);

    // set the patient name
dataSet.setUnicodePatientName(imebra::TagId(imebra::tagId_t::PatientName_0010_0010),imebra::UnicodePatientName(L"fjx",L"",L""));

    // save to a file
    imebra::CodecFactory::save(dataSet,"GH1.dcm",imebra::codecType_t::dicom);
    free(pImgData);
}

当我使用 gdb 调试我的代码时,出现了问题,我将堆栈大小更改为 100MB,但随后会发生分段错误。 也许动态内存应用有问题?? 谁能帮帮我? 仅供参考,imebra::XXX 的功能都来自 imebra lib。

【问题讨论】:

  • 尝试使用 valgrind 来找出覆盖内存的位置。提示:阅读fread的手册页。
  • 不相关,但 writeIntoImage 和循环应该在 {} 中,所以 writeIntoImage 超出范围并更新图像内容
  • @PaoloBrandoli,你的意思是“imebra ::MutableImage image(img_height,img_width,imebra::bitDepth_t::depthU16,"MONOCHROME2",15);"不应该带“;” ,下面的代码应该在 {}?

标签: c++ linux segmentation-fault stack imebra


【解决方案1】:

你不能在new分配的内存上调用free。这会导致未定义的行为。您必须改为致电delete

您也只分配 一个 uint32_t(并使用值 img_size 对其进行初始化),而不是 img_size 的数组。为此,您将需要new (std::nothrow) uint32_t[img_size];(以及后来的delete[] 而不是delete)。所以你要用fread写越界。

您还需要检查new(std::nothrow) 的返回值是否不是空指针,这会在分配失败时发生。如果您使用投掷版本,则不需要该检查。

请不要像这样使用new,而是使用std::vector。 C++ 中的malloc 甚至比new 还要糟糕。

同样,不要在 C++ 中使用 C IO 库。请改用std::ifstream

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多