【问题标题】:Double free or memory allocation error双重释放或内存分配错误
【发布时间】:2016-05-20 17:44:53
【问题描述】:

我是 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&lt;int&gt;(r,c) = 0; 行,就不会出现任何错误。

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

我们非常感谢这方面的任何帮助。

【问题讨论】:

    标签: c++ opencv out-of-memory


    【解决方案1】:

    您的 Mat 的类型为 CV_8UC1,它是一个 8 位 = 1 字节的数据类型。

    您尝试以 .at&lt;int&gt; 的身份访问,但 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
            }
        }
       }
    }
    

    【讨论】:

    • 非常感谢您的解释,我很难理解它是如何工作的。谢谢
    猜你喜欢
    • 2017-03-22
    • 2021-11-25
    • 2018-01-29
    • 2016-11-06
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多