Double free or corruption (!prev) error in my c++ destructor

I have a program that does operations on a vector, not altering them but just reading once and then writing out what is required based on what is given. My program runs but I get this error at the end;

*** Error in './volimage: double free or corruption (!prev): 0x00000000123c10 ***

I have googled, and it seems the problem is with my destructor, but I can't for the life of me find or solve it.

This is the destructor:

VolImage::~VolImage()
{
    std::cout << "Destructor" << std::endl;
    //deconstructing the vector of slices
    int i, j, k;
    for (i = 0; i<size; i++)
    {
        for (j = 0; j<height; j++)
        {
            delete[] slices[i][j];
        }
        delete[] slices[i]; 
    }
    slices.clear();
}

The vector was populated using the following:

std::vector<unsigned char**> slices; // data for each slice, in order

//populating vector
int i, j, k;
unsigned char ** rows = new unsigned char*[height];
for (i = 0; i < size; i++) 
{
    for (j = 0; j < height; j++) 
    {
        unsigned char* cols = new unsigned char[width];
        string num = "%d" + j;
        fileName = baseName + num + ".raw";
        myFile.open(fileName);
        for (k = 0; k < width; k++)
        {
            unsigned char x;
            myFile >> x;
            cols[k] = x;
        }
        myFile.close();
        rows[i] = cols;
    }
    slices.push_back(rows);
}

Thanks, a hasty reply would be appreciated as i need to submit this soon

Upvotes: 0

Views: 1340

Answers (2)

user3554704
user3554704

Reputation: 1

This is not a direct answer, I agree with @MikeCAT's answer. I want to add to the comment posted under the soln : (I dont have enough rep to post a comment directly). Please see here for why sizeof(slices) returns 24.

How about something like : (width * height * sizeof(slices[0][0][0]) ) * size, provided that slices[0][0][0] exists.

Upvotes: -1

MikeCAT
MikeCAT

Reputation: 75062

You allocated the buffer for storing pointers to pointers only once. You have to allocate ones for each rows.

Also note that the line string num = "%d" + j; has a big chance to cause out-of-range access because "%d" + j is equivalent to &"%d"[j] and only 0 <= j < 3 is allowed.

One more thing: rows[i] = cols; should be rows[j] = cols; as @dasblinkenlight says.

Try this:

//populating vector
int i, j, k;
for (i = 0; i < size; i++) 
{
    unsigned char ** rows = new unsigned char*[height]; // move this line
    for (j = 0; j < height; j++) 
    {
        unsigned char* cols = new unsigned char[width];
        std::stringstream ss;
        string num;
        ss << j;
        ss >> num; // convert the integer stored in j to string
        fileName = baseName + num + ".raw";
        myFile.open(fileName);
        for (k = 0; k < width; k++)
        {
            unsigned char x;
            myFile >> x;
            cols[k] = x;
        }
        myFile.close();
        rows[j] = cols;
    }
    slices.push_back(rows);
}

Add #include <sstream> to your code if it doesn't exists to use std::stringstream.

Upvotes: 2

Related Questions