std::vector在filterAcceptsRow内部调用size()后变为无效

std::vector becomes invalid after calling size() on it inside filterAcceptsRow

本文关键字:无效 调用 vector filterAcceptsRow 内部 std size      更新时间:2023-10-16

我正在使用自制的QSortFilterProxyModel对应用程序中的模型进行筛选和排序。

以下是filterAcceptsRow函数:

//---------------------------------------------------------------------------------
bool
SkillSortFilterProxyModel::filterAcceptsRow(int p_row, const QModelIndex& p_parent) const
{
    QModelIndex currentIndex = sourceModel()->index(p_row, 0, p_parent);
    SkillModelItem* currentItem = (SkillModelItem*)currentIndex.internalPointer();
    // Check the item
    bool accept = filterAcceptsItem(currentItem);
    // We may hide a category if it is empty
    if (accept && currentItem->isCategory && !_showEmptyCategories)
    {
        // Iterate over the children to find out if any will be accepted
        // And if so, accept the category
        int numChildren = currentItem->children.size();
        accept = false;
        for (int i = 0; i < numChildren; ++i)
        {
            SkillModelItem* item = currentItem->children[i];
            if (filterAcceptsItem(item))
            {
                accept = true;
                break;
            }
        }
    }
    // Return result
    return accept; 
}

它应该做的是:我认为SkillModelItem的确切性质在这里并不重要,但你应该明白,相同的模型项类用于技能类别和技能本身。filterAcceptsRow函数调用filterAccept项目以查看是否应显示特定项目。这很有效。

但是,如果项目是一个类别,则还应检查其子项,以查看是否有任何可接受的子项,如果有,则应显示该类别。

理论上应该有效,但实际情况是,在调用currentItem->children.size()之后,currentItem->children(std::vector)将无效!它返回正确的大小,但如果我再次调用它,大小现在是一些随机数。以及在应用程序崩溃后访问for循环中的子项。

我不知道这里发生了什么。应用程序没有线程化(至少我没有使用任何线程)。我在Windows上使用Qt Creator,使用MinGW作为编译器。我也尝试过使用MSVC,但它甚至不会编译,因为它声称找不到任何头文件(MinGW可以毫无问题地找到)。还尝试重新编译、重新运行qmake等,但都无济于事。

你知道这里可能出了什么问题吗?

如果有帮助,你可以在这里查看来源:GitHub repo

原来这是一个调试器问题。

如果我不在运行时逐步完成,该函数就可以正常工作。

Qt Creator或随附的MinGW版本中似乎有一个错误。

相关文章: