【发布时间】:2015-03-08 20:25:59
【问题描述】:
我收到此错误(内存位置因运行而异):
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,我该如何调试?此外,有关如何解决问题的提示也会非常有帮助。
编辑:我正在运行输出文件,有时它会运行,而另一些则给我错误。似乎是随机顺序。此外,当我在 gdb 上运行该程序时,它永远不会失败,它总是正确退出。为什么会这样?
【问题讨论】:
标签: c++ memory-leaks