我的 C++ 析构函数中出现双重释放或损坏 (!prev) 错误
Double free or corruption (!prev) error in my c++ destructor
我有一个程序可以对向量进行操作,不改变它们而只是读取一次,然后根据给定的内容写出所需的内容。我的程序运行但最后出现此错误;
*** Error in './volimage: double free or corruption (!prev): 0x00000000123c10 ***
我用谷歌搜索了一下,似乎问题出在我的析构函数上,但我终究找不到或解决它。
这是析构函数:
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();
}
使用以下内容填充矢量:
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);
}
谢谢,请尽快回复,因为我需要尽快提交
您只分配了一次用于存储指针的缓冲区。您必须为每一行分配一个。
另请注意,string num = "%d" + j;
行很有可能导致越界访问,因为"%d" + j
等同于&"%d"[j]
,且只允许0 <= j < 3
.
还有一件事:rows[i] = cols;
应该是 rows[j] = cols;
正如@dasblinkenlight 所说。
试试这个:
//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);
}
如果 #include <sstream>
不存在,请将其添加到您的代码中以使用 std::stringstream
。
这不是直接的回答,我同意@MikeCAT 的回答。我想在 soln 下添加评论 posted:(我没有足够的代表直接 post 发表评论)。
为什么 sizeof(slices)
returns 24.
怎么样:(width * height * sizeof(slices[0][0][0]) ) * size
,前提是 slices[0][0][0]
存在。
我有一个程序可以对向量进行操作,不改变它们而只是读取一次,然后根据给定的内容写出所需的内容。我的程序运行但最后出现此错误;
*** Error in './volimage: double free or corruption (!prev): 0x00000000123c10 ***
我用谷歌搜索了一下,似乎问题出在我的析构函数上,但我终究找不到或解决它。
这是析构函数:
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();
}
使用以下内容填充矢量:
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);
}
谢谢,请尽快回复,因为我需要尽快提交
您只分配了一次用于存储指针的缓冲区。您必须为每一行分配一个。
另请注意,string num = "%d" + j;
行很有可能导致越界访问,因为"%d" + j
等同于&"%d"[j]
,且只允许0 <= j < 3
.
还有一件事:rows[i] = cols;
应该是 rows[j] = cols;
正如@dasblinkenlight 所说。
试试这个:
//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);
}
如果 #include <sstream>
不存在,请将其添加到您的代码中以使用 std::stringstream
。
这不是直接的回答,我同意@MikeCAT 的回答。我想在 soln 下添加评论 posted:(我没有足够的代表直接 post 发表评论)。
sizeof(slices)
returns 24.
怎么样:(width * height * sizeof(slices[0][0][0]) ) * size
,前提是 slices[0][0][0]
存在。