双空闲或内存分配错误

Double free or memory allocation error

本文关键字:分配 错误 内存      更新时间:2023-10-16

我是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;行,就不会有任何错误。

我不明白这一点,因为rc分别只是矩阵行号和列号的obstacles

非常感谢这方面的

任何帮助。

您的垫子的类型CV_8UC1是8位= 1字节数据类型。

您尝试以.at<int>身份访问,但 int 是 32 位数据类型。

请尝试使用无符号字符或其他 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
        }
    }
   }
}