给出一些背景:在我的项目中,我在QMap::detach_helper中放置了一个调试断点,因为我想知道当隐式共享QMaps由于疏忽而分离时,是否能够发现任何事件,例如,在可能使用constFind时使用find。我没想到会经常碰到它,因为我主要是通过const引用传递容器(顺便指出,显然有一个叫做"clazy“的工具可以找到这样的东西)。
然后,我查看了触发分离的一些内部Qtv5.9.3代码。堆栈跟踪显示,我们脱离了insertMulti函数的第一行,这是在contexts上调用的。
// return true if accepted (consumed)
bool QGestureManager::filterEvent(QWidget *receiver, QEvent *event)
{
QMap<Qt::GestureType, int> types;
QMultiMap<QObject *, Qt::GestureType> contexts;
QWidget *w = receiver;
typedef QMap<Qt::GestureType, Qt::GestureFlags>::const_iterator ContextIterator;
if (!w->d_func()->gestureContext.isEmpty()) {
for(ContextIterator it = w->d_func()->gestureContext.constBegin(),
e = w->d_func()->gestureContext.constEnd(); it != e; ++it) {
types.insert(it.key(), 0);
contexts.insertMulti(w, it.key());
}
}
// find all gesture contexts for the widget tree
w = w->isWindow() ? 0 : w->parentWidget();
while (w)
{
for (ContextIterator it = w->d_func()->gestureContext.constBegin(),
e = w->d_func()->gestureContext.constEnd(); it != e; ++it) {
if (!(it.value() & Qt::DontStartGestureOnChildren)) {
if (!types.contains(it.key())) {
types.insert(it.key(), 0);
contexts.insertMulti(w, it.key()); // Why does this trigger a detach?
}
}
}
if (w->isWindow())
break;
w = w->parentWidget();
}
return contexts.isEmpty() ? false : filterEventThroughContexts(contexts, event);
}为什么本地QMultiMap contexts--从未被复制过--会隐式地共享并且需要分离?
我的理论
这可能与此无关,但在这一行中,contexts的大小为零。
我的猜测是,分离是由某种与空映射相关的优化引起的,但我不确定。我确实注意到,通过将调试断点放置在QMap::detach_helper的部分(只对非空映射执行)(即在条件if (d->header.left)中),我得到的点击次数要少得多。
发布于 2021-06-20 08:09:12
Q(Multi)Map并不是在每次插入时分离,而是只在映射尚未初始化时才分离:
QMultiMap<int, int> mm;
mm.insert(42, 43); // detach_helper is called because the container needs to be initialized
mm.insert(43, 44); // detach_helper is not calledhttps://stackoverflow.com/questions/68043671
复制相似问题