释放对象的校验和不正确
incorrect checksum for freed object
我收到此错误(内存位置在 运行 秒之间变化):
q2(4910,0x7fff7a1d4300) malloc: *** error for object 0x7fdf79c04bd8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
这是崩溃的函数:
public:
// construct a 'rows X cols' matrix.
SMatrix(int rows, int cols) {
if (rows<1 || cols<1) {
cout<<"Invalid row/col value(s).";
exit(-1);
}
this->_rows = rows;
this->_cols = cols;
this->_vertical = new simpleNode [rows];
this->_horizontal = new simpleNode [cols];
if (this->_vertical == NULL || this->_horizontal==NULL) {
cout<<"Exiting";
exit(-1);
}
initArrays();
}
它在这一行崩溃:
this->_horizontal = new simpleNode [cols];
调用的函数:
int main() {
SMatrix bigM(500,500);
bigM.setElement(10,20,17);
cout <<" bigM - total size in bytes: (implementation depended): "
<< bigM.sizeInBytes() << endl << endl;
SMatrix m1(7,10),m2(7,10),m4(10,2),m5(7,2); //Crashes on m4(10,2)
}
其他可能相关的功能:
struct simpleNode {
Node* _next;
};
int _rows; //Number of rows in this SMatrix
int _cols; //Number of columns in this SMatrix
simpleNode * _vertical; //array (simpleNode)
simpleNode * _horizontal; //array (simpleNode)
/*Initiate the horizontal/vertical arrays to point to null*/
void initArrays() {
int i;
for (i=0; i<this->_rows; i++)
this->_horizontal[i]._next = NULL;
for (i=0; i<this->_cols; i++)
this->_vertical[i]._next = NULL;
}
我在 OSX。我用 -g 和 运行 用 GDB 编译,但 程序正常退出。
如果我不使用 XCode,我该如何调试它?另外,关于如何解决问题的提示也会很有帮助。
编辑:我正在 运行 输出文件,有时它 运行s 而其他人它给我错误。似乎处于 运行dom 顺序。此外,当我在 gdb 上 运行 它总是正确退出时,该程序永远不会失败。为什么会这样?
既然你在调试器中,你应该查看内存位置 0x7fff7a1d4300
看看那里有什么。内存中的数据可能有助于找出问题所在。
发生的情况是以下情况之一:
您正在释放一个对象两次,
您正在释放一个从未分配过的指针
您正在通过一个无效指针进行写入,该指针之前指向一个已被释放的对象
我认为发生的事情是No.3。
我的答案基于 this 答案。
相关讨论位于here。
关于 gdb 的相关 question。
您的限制在您的初始化代码中被颠倒了。您可以这样创建数组:
this->_vertical = new simpleNode [rows]; // <== uses rows for sizing vertical
this->_horizontal = new simpleNode [cols]; // <== uses cols for sizing horizontal
但是你的初始化是这样的:
for (i=0; i<this->_rows; i++) // <== limit is rows, but you walking horizontal
this->_horizontal[i]._next = NULL;
for (i=0; i<this->_cols; i++) // <== limit is cols, but you walking vertical
this->_vertical[i]._next = NULL;
除非 rows
和 cols
是相同的值,否则此代码会调用 未定义的行为 。通过使用与
分配大小相同的值来解决此问题
for (i=0; i<this->_rows; i++)
this->_vertical[i]._next = NULL;
for (i=0; i<this->_cols; i++)
this->_horizontal[i]._next = NULL;
老实说,更好的方法是使用 RAII 容器,例如 std::vector<>
,但我将其作为练习留给您。
祝你好运,希望对你有所帮助。
我收到此错误(内存位置在 运行 秒之间变化):
q2(4910,0x7fff7a1d4300) malloc: *** error for object 0x7fdf79c04bd8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
这是崩溃的函数:
public:
// construct a 'rows X cols' matrix.
SMatrix(int rows, int cols) {
if (rows<1 || cols<1) {
cout<<"Invalid row/col value(s).";
exit(-1);
}
this->_rows = rows;
this->_cols = cols;
this->_vertical = new simpleNode [rows];
this->_horizontal = new simpleNode [cols];
if (this->_vertical == NULL || this->_horizontal==NULL) {
cout<<"Exiting";
exit(-1);
}
initArrays();
}
它在这一行崩溃:
this->_horizontal = new simpleNode [cols];
调用的函数:
int main() {
SMatrix bigM(500,500);
bigM.setElement(10,20,17);
cout <<" bigM - total size in bytes: (implementation depended): "
<< bigM.sizeInBytes() << endl << endl;
SMatrix m1(7,10),m2(7,10),m4(10,2),m5(7,2); //Crashes on m4(10,2)
}
其他可能相关的功能:
struct simpleNode {
Node* _next;
};
int _rows; //Number of rows in this SMatrix
int _cols; //Number of columns in this SMatrix
simpleNode * _vertical; //array (simpleNode)
simpleNode * _horizontal; //array (simpleNode)
/*Initiate the horizontal/vertical arrays to point to null*/
void initArrays() {
int i;
for (i=0; i<this->_rows; i++)
this->_horizontal[i]._next = NULL;
for (i=0; i<this->_cols; i++)
this->_vertical[i]._next = NULL;
}
我在 OSX。我用 -g 和 运行 用 GDB 编译,但 程序正常退出。 如果我不使用 XCode,我该如何调试它?另外,关于如何解决问题的提示也会很有帮助。
编辑:我正在 运行 输出文件,有时它 运行s 而其他人它给我错误。似乎处于 运行dom 顺序。此外,当我在 gdb 上 运行 它总是正确退出时,该程序永远不会失败。为什么会这样?
既然你在调试器中,你应该查看内存位置 0x7fff7a1d4300
看看那里有什么。内存中的数据可能有助于找出问题所在。
发生的情况是以下情况之一:
您正在释放一个对象两次,
您正在释放一个从未分配过的指针
您正在通过一个无效指针进行写入,该指针之前指向一个已被释放的对象
我认为发生的事情是No.3。
我的答案基于 this 答案。
相关讨论位于here。
关于 gdb 的相关 question。
您的限制在您的初始化代码中被颠倒了。您可以这样创建数组:
this->_vertical = new simpleNode [rows]; // <== uses rows for sizing vertical
this->_horizontal = new simpleNode [cols]; // <== uses cols for sizing horizontal
但是你的初始化是这样的:
for (i=0; i<this->_rows; i++) // <== limit is rows, but you walking horizontal
this->_horizontal[i]._next = NULL;
for (i=0; i<this->_cols; i++) // <== limit is cols, but you walking vertical
this->_vertical[i]._next = NULL;
除非 rows
和 cols
是相同的值,否则此代码会调用 未定义的行为 。通过使用与
for (i=0; i<this->_rows; i++)
this->_vertical[i]._next = NULL;
for (i=0; i<this->_cols; i++)
this->_horizontal[i]._next = NULL;
老实说,更好的方法是使用 RAII 容器,例如 std::vector<>
,但我将其作为练习留给您。
祝你好运,希望对你有所帮助。