简体   繁体   English

std::set 整数插入上的段错误

[英]Seg fault on std::set integer insert

I have a function that compares vectors in a vector and merges them if they are similar.我有一个函数可以比较向量中的向量并在它们相似时合并它们。 I have to remember the indices of array elements that were already merged, so I save them in a queue.我必须记住已经合并的数组元素的索引,所以我将它们保存在队列中。

On some random elements the insert of the index into the queue produces a seg fault.在某些随机元素上,将索引插入队列会产生段错误。 I really don't get why.我真的不明白为什么。 Here is my code:这是我的代码:

using namespace cv;
using namespace std;
//...
//...
unordered_set<uint> mergedIdxSet = unordered_set<uint>();
mergedIdxSet.reserve(regions.size());
//...
//...
cout << "Test region " << r1 << " (" << regions[r1].size() << ") with...\n";
for(uint r2 = r1+1; r2 < regions.size(); r2++)
{
    if((bBoxes[r1] & bBoxes[r2]).area() == 0)
    {
        continue;
    }

    cout << "\tRegion " << r2 << "\n";

    Rect unionBound = bBoxes[r1] | bBoxes[r2];
    Point upperLeftBound = Point(unionBound.x, unionBound.y);
    Mat interMap = Mat::zeros(unionBound.size(), CV_8UC1);
    for(uint p = 0; p < regions[r1].size(); p++)
    {
        interMap.at<uchar>(regions[r1][p]-upperLeftBound) = 128;
    }
    for(uint p = 0; p < regions[r2].size(); p++)
    {
        interMap.at<uchar>(regions[r2][p]-upperLeftBound) += 127;
    }
    int intersectionArea = countNonZero(interMap);
    if(intersectionArea / regions[r1].size() >= ratio ||
       intersectionArea / regions[r2].size() >= ratio)
    {
        cout << "\t\tMerge\n";
        mergedIdxSet.insert(r2);
        for(uint i = 0; i < regions[r2].size(); i++)
        {
            pointSet.insert(regions[r2][i]);
        }
        regions[r1] = vector<Point>(pointSet.begin(), pointSet.end());
        cout << "New length: " << regions[r1].size() << "\n";
    }
}

The algorithm begins with region 0 and merges regions 1, 2, 3, 4, 5, 6 and 8. Then it wants to merge region 685 and crashes upon adding the unsigned int 685 in variable r2 into mergedIdxSet.该算法从区域 0 开始并合并区域 1、2、3、4、5、6 和 8。然后它想要合并区域 685,并在将变量 r2 中的 unsigned int 685 添加到mergedIdxSet 时崩溃。 If I just skip the insert it crashes when trying to insert regions[r2] into pointSet.如果我只是跳过插入,它会在尝试将区域 [r2] 插入点集时崩溃。 If I skip the region 685 it crashes on some other region much later.如果我跳过区域 685,它会在很久以后在其他区域崩溃。 Why does that happen?为什么会这样?

Thanks in advance.提前致谢。

The problem was that I tried to overwrite the vector in regions[r1].问题是我试图覆盖区域 [r1] 中的向量。 Somehow that caused the whole program to crash at the queue insert.不知何故,导致整个程序在队列插入时崩溃。 I replaced the merging process by deleting the very similar regions and it works now.我通过删除非常相似的区域来替换合并过程,现在它可以工作了。

Thanks for the input.感谢您的投入。

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

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