双重释放或内存分配错误
Double free or memory allocation error
我是C++编程的新手,我在我的一部分代码中遇到了这个问题,错误有时是内存分配类型错误,有时是双重释放错误。
错误代码如下
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<int>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<int>(r,c) = 0; //error is in this line
}
}
}
}
如果我删除 obstacles.at<int>(r,c) = 0;
行,将不会有任何错误。
我不明白这个,因为r
和c
分别只是obstacles
矩阵的行数和列数。
非常感谢在这方面的任何帮助。
您的 Mat 类型为 CV_8UC1
,这是一个 8 位 = 1 字节的数据类型。
您尝试以 .at<int>
访问,但 int 是 32 位数据类型。
请尝试使用 unsigned char 或其他 8 位类型,如下所示:
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<uchar>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<uchar>(r,c) = 0; //error is in this line
}
}
}
}
我是C++编程的新手,我在我的一部分代码中遇到了这个问题,错误有时是内存分配类型错误,有时是双重释放错误。
错误代码如下
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<int>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<int>(r,c) = 0; //error is in this line
}
}
}
}
如果我删除 obstacles.at<int>(r,c) = 0;
行,将不会有任何错误。
我不明白这个,因为r
和c
分别只是obstacles
矩阵的行数和列数。
非常感谢在这方面的任何帮助。
您的 Mat 类型为 CV_8UC1
,这是一个 8 位 = 1 字节的数据类型。
您尝试以 .at<int>
访问,但 int 是 32 位数据类型。
请尝试使用 unsigned char 或其他 8 位类型,如下所示:
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<uchar>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<uchar>(r,c) = 0; //error is in this line
}
}
}
}