简体   繁体   English

调试断言错误 - 列表迭代器不兼容

[英]Debug assertion error - List iterators incompatible

I'm working on a program that is supposed to put every Window in a list, resize it, and move it to a screen position according to a specified layout.我正在开发一个程序,该程序应该将每个 Window 放入列表中,调整其大小,然后根据指定的布局将其移动到屏幕 position 上。

When I'm running this function however I get a debug assertion error saying "list iterators incompatible".当我运行这个 function 但是我得到一个调试断言错误,说“列表迭代器不兼容”。

Here is the code:这是代码:

void Control::checkForNewWindows()
{
    for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); i != mainDetector.getWindowList().end(); ++i)
    {
        bool forBreak = false;
        if ((i->getTitle().find("sample_title") != std::string::npos) && (i->getState() == false))
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (activeLayout.windowLayout[y][x].getHandle() == 0)
                    {
                        moveWindow(*i, activeLayout.dimensionsLayout[y][x].x, activeLayout.dimensionsLayout[y][x].y, activeLayout.dimensionsLayout[y][x].width,
                            activeLayout.dimensionsLayout[y][x].height);
                        activeLayout.windowLayout[y][x] = *i;
                        activeLayout.windowLayout[y][x].setState(true);
                        forBreak = true;
                    }
                    if (forBreak)
                    {
                        break;
                    }
                }
                if (forBreak)
                {
                    break;
                }
            }
        }
    }
}

The error occurs during the first for loop, hope someone can help me fix this错误发生在第一个for循环期间,希望有人能帮我解决这个问题

Edit:编辑:

Here is the getWindowList function:这是 getWindowList function:

std::list <Window> Detector::getWindowList()
{
    return windowList;
}

and the windowList definition:和 windowList 定义:

std::list <Window> windowList;

Your loop looks like this: 您的循环如下所示:

for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); 
     i != mainDetector.getWindowList().end(); 
     ++i)

Given the above, the issue is this: 鉴于上述情况,问题是这样的:

std::list <Window> Detector::getWindowList()
{
    return windowList;
}

You're returning a copy of the list, not the original. 您将返回列表的副本,而不是原始副本。 Thus the iterator to the copy will be used in the loop and not the iterator of windowList . 因此,副本的迭代器将在循环中使用,而不是windowList的迭代器。 In fact, you are using two different iterators in the loop construct, and neither one of them refers to the original list, only copies. 实际上,您在循环结构中使用了两个不同的迭代器,它们中的任何一个都不引用原始列表,仅引用副本。

The fix is to return a reference: 解决方法是返回参考:

std::list <Window>& Detector::getWindowList()
{
    return windowList;
}

You're now returning a reference to the actual list, not a copy. 您现在返回的是对实际列表的引用,而不是副本。 Now the iterators you are using in the loop constraints refer to the same list, not different lists. 现在,您在循环约束中使用的迭代器引用的是同一列表,而不是不同的列表。

FYI for others researching this, you can also get the same error message when dealing with dangling pointers and deleted memory.仅供研究此问题的其他人使用,您在处理悬空指针并删除 memory 时也会收到相同的错误消息。 I ran across this thread while tracking down that error message.我在追踪该错误消息时遇到了这个线程。 In my case, there was an iterator to a list that had been deleted.就我而言,有一个迭代器指向已被删除的列表。

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

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