lee1591
lee1591

Reputation: 17

segmentation fault when compling c++ code

I am now days struggling at this question from my code,the code could be complied successfully but when I ran the binary file, the segmentation fault would occured and here below was the problem:

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 workstation Com: g++, version:5.4.0 c++:c++11 Lib: imebra 5.0.1 Here is my code:

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

When I use gdb to debug my code, the question occured and I have changed my stack size to 100MB,but then the segementation fault would occure. Maybe something wrong with Dynamic memory application?? Could anyone help me out? FYI,The func of imebra::XXX are all from imebra lib.

Upvotes: 0

Views: 109

Answers (1)

walnut
walnut

Reputation: 22162

You are not allowed to call free on memory allocated by new. That causes undefined behavior. You must call delete instead.

You are also allocating only one uint32_t (and initialing it with the value img_size), not an array of img_size many. For that you would need new (std::nothrow) uint32_t[img_size]; instead (and later delete[] instead of delete). So you are going to write out-of-bounds with fread.

You also need to check that the return value of new(std::nothrow) is not a null pointer, which would happen on allocation failure. If you use the throwing version, then you won't need that check.

Please don't use new like this though and use std::vector instead. malloc in C++ is even worse than new.

Similarly, don't use the C IO library in C++. Use std::ifstream instead.

Upvotes: 4

Related Questions