简体   繁体   English

OpenCV错误:函数调用中的内存不足

[英]OpenCV Error: insufficient memory, in function call

I have a function looks like this: 我有一个函数看起来像这样:

void foo(){
     Mat mat(50000, 200, CV_32FC1);

     /* some manipulation using mat */

     }

Then after several loops (in each loop, I call foo() once), it gives an error: 然后经过几个循环(在每个循环中,我都调用foo()一次),它给出了一个错误:

OpenCV Error: insufficient memory when allocating (about 1G) memory. OpenCV错误:分配(大约1G)内存时内存不足。

In my understanding, the Mat is local and once foo() returns, it is automatically de-allocated, so I am wondering why it leaks. 据我了解, Mat是本地的,一旦foo()返回,它就会自动取消分配,所以我想知道为什么它会泄漏。

And it leaks on some data, but not all of them. 而且它泄漏了一些数据,但不是全部。

Here is my actual code: 这是我的实际代码:

bool VidBOW::readFeatPoints(int sidx, int eidx, cv::Mat &keys, cv::Mat &descs, cv::Mat &codes, int &barrier) {
    // initialize buffers for keys and descriptors
    int num = 50000; /// a large number
    int nDims = 0; /// feature dimensions


    if (featName == "STIP")
        nDims = 162;

    Mat descsBuff(num, nDims, CV_32FC1);
    Mat keysBuff(num, 3, CV_32FC1);
    Mat codesBuff(num, 3000, CV_64FC1);

    // move overlapping codes from a previous window to buffer
    int idxPre = -1;
    int numPre = keys.rows;
    int numMov = 0; /// number of overlapping points to move

    for (int i = 0; i < numPre; ++i) {
        if (keys.at<float>(i, 0) >= sidx) {
            idxPre = i;
            break;
        }
    }

    if (idxPre > 0) {
        numMov = numPre - idxPre;
        keys.rowRange(idxPre, numPre).copyTo(keysBuff.rowRange(0, numMov));
        codes.rowRange(idxPre, numPre).copyTo(codesBuff.rowRange(0, numMov));
    }

    // the starting row in code matrix where new codes from the updated features to add in
    barrier = numMov;

    // read keys and descriptors from feature file
    int count = 0; /// number of new points that are read in buffers
    if (featName == "STIP")
        count = readSTIPFeatPoints(numMov, eidx, keysBuff, descsBuff);

    // update keys, descriptors and codes matrix
    descsBuff.rowRange(0, count).copyTo(descs);
    keysBuff.rowRange(0, numMov+count).copyTo(keys);
    codesBuff.rowRange(0, numMov+count).copyTo(codes);

    // see if reaching the end of a feature file
    bool flag = false;

    if (feof(fpfeat))
        flag = true;

    return flag;
}

You don't post the code that calls your function, so I can't tell whether this is a true memory leak. 您不会发布调用函数的代码,因此我无法确定这是否是真正的内存泄漏。 The Mat objects that you allocate inside readFeatPoints() will be deallocated correctly, so there are no memory leaks that I can see. 您在readFeatPoints()内部分配的Mat对象将被正确地重新分配,因此我看不到任何内存泄漏。

You declare Mat codesBuff(num, 3000, CV_64FC1); 您声明Mat codesBuff(num, 3000, CV_64FC1); . With num = 5000 , this means you're trying to allocate 1.2 gigabytes of memory in one big block. num = 5000 ,这意味着您试图在一个大块中分配1.2 GB的内存。 You also copy some of this data to codes with the line: 您还可以将以下数据复制到以下codes行中:

codesBuff.rowRange(0, numMov+count).copyTo(codes);

If the value of numMove + count changes between iterations, this will cause reallocation of the data buffer in codes . 如果numMove + count的值在numMove + count迭代之间发生变化,则将导致codes中的数据缓冲区重新分配。 If the value is large enough, you may also be eating up a significant amount of memory that persists across iterations of your loop. 如果该值足够大,则可能还会消耗大量内存,这些内存会在循环的迭代过程中持续存在。 Both of these things may be leading to heap fragmentation . 这两件事都可能导致 碎片化 If at any point there doesn't exist a 1.2 GB chunk of memory waiting around, an insufficient memory error occurs, which is what you have experienced. 如果在任何时候都不存在等待1.2 GB的内存块,则会发生内存不足错误,这就是您遇到的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM