std::vector在filterAcceptsRow内部调用size()后变为无效
std::vector becomes invalid after calling size() on it inside filterAcceptsRow
我正在使用自制的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版本中似乎有一个错误。
相关文章:
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 模板和无效函数调用C++
- JNI 在应用程序中检测到错误:在为 innerclass 调用 NewObject 时使用了无效的 jobject
- 在抛出 what() 的实例后调用'std::logic_error'终止:basic_string::_M_construct 空无效
- cppcheck 抱怨危险地使用 c_str(). c_str() 返回的值在此调用后无效
- cpp 检查抱怨危险使用 c_str(). c_str() 返回的值在本次调用后无效,如何解决?
- 为什么构造函数的虚拟函数调用有时有效,但其他调用却无效
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- 函数中的"pragma omp parallel for"在另一个并行循环中调用函数时无效
- 无法确定为什么函数调用中从 char* 到 char 的无效转换
- 由于调用 std::condition_variable 后参数无效而导致应用程序崩溃
- MPI_Iprobe:RMA 调用中的位移参数无效
- 抛出 'std::logic_error' basic_string::_S_construct null 无效的实例后调用的终止
- free():多次调用后下一个大小(快速)无效
- 编译器错过了无效的构造函数调用,并调用不存在的(或私有的)默认构造函数
- 从 WindowsAPI 调用 NetLocalGroupAddMembers 时,安全 ID 结构无效错误
- 汇编错误:调用基本型构造函数时无效的转换
- 如何修复抛出"std::logic_error"实例后调用的终止 what(): basic_string::_M_construct null 无效
- 调用 GetPointerFrameTouchInfo时出现错误 998(对内存位置的无效访问)
- C++无效调用超时