簡體   English   中英

OpenCV錯誤:函數調用中的內存不足

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

我有一個函數看起來像這樣:

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

     /* some manipulation using mat */

     }

然后經過幾個循環(在每個循環中,我都調用foo()一次),它給出了一個錯誤:

OpenCV錯誤:分配(大約1G)內存時內存不足。

據我了解, Mat是本地的,一旦foo()返回,它就會自動取消分配,所以我想知道為什么它會泄漏。

而且它泄漏了一些數據,但不是全部。

這是我的實際代碼:

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;
}

您不會發布調用函數的代碼,因此我無法確定這是否是真正的內存泄漏。 您在readFeatPoints()內部分配的Mat對象將被正確地重新分配,因此我看不到任何內存泄漏。

您聲明Mat codesBuff(num, 3000, CV_64FC1); num = 5000 ,這意味着您試圖在一個大塊中分配1.2 GB的內存。 您還可以將以下數據復制到以下codes行中:

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

如果numMove + count的值在numMove + count迭代之間發生變化,則將導致codes中的數據緩沖區重新分配。 如果該值足夠大,則可能還會消耗大量內存,這些內存會在循環的迭代過程中持續存在。 這兩件事都可能導致 碎片化 如果在任何時候都不存在等待1.2 GB的內存塊,則會發生內存不足錯誤,這就是您遇到的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM