Program crashes because of some assertion in <qhash.h>
-
I get access violations frequently when running the debug version of my program, but for release version, it almost never crashes.
It seems my code has some problem with QHash usage, when program crashes, the debug info leads me to:QHash<QString, QVariant>::findNode in line 884 of qhash.h, which is a Q_ASSERT statement
@
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey,
uint *ahp) const
{
Node **node;
uint h = qHash(akey);if (d->numBuckets) { node = reinterpret_cast<Node **>(&d->buckets[h % d->numBuckets]); Q_ASSERT(*node == e || (*node)->next); //line 884 while (*node != e && !(*node)->same_key(h, akey)) node = &(*node)->next; } else { node = const_cast<Node **>(reinterpret_cast<const Node * const *>(&e)); } if (ahp) *ahp = h; return node;
}
@QHash<QString, QVariant>::value in line 618 of qhash.h,
@
template <class Key, class T>
Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey, const T &adefaultValue) const
{
Node *node;
if (d->size == 0 || (node = *findNode(akey)) == e) { //line 618
return adefaultValue;
} else {
return node->value;
}
}
@Some more information about my program:
I use a QList of QHash<QString, QVariant> to store data which will be displayed in a QTreeView, and a QTimer thread updates the data every second, the error always takes place when some QHash data is being updated.I don't have a clue, please give me some hints how this takes place, and what kind of misuse of QHash may cause this error.
Thanks for help.
-
Lets start with the assumption that the error is in your program :-) That is usually a save bet.
I would guess you are accessing something that is somehow invalid, perhaps the hash itself. That could be because of your threading. Did you protect access to the data structure using mutexes or something like that so that your threads can savely access the data?
-
Indeed, some more information about your usage would be nice.
- Are you accessing QHash from another thread?
- Are you sure the instance you're accessing was not delete before? (Corrupted memory)
I've done pretty much with QHash and I can tell you it works perfectly ;)
-
[quote]
I use a QList of QHash<QString, QVariant> to store data which will be displayed in a QTreeView, and a QTimer thread updates the data every second, the error always takes place when some QHash data is being updated.
[/quote]A QTimer thread? Are you using "threads for timers":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#92dd35cc61ffc41d02defdcef071856d ?
-
OK, here's some more information about my program.
-
I didn't use mutexes to protect my data, but since I had only one thread that modifies data, and QTreeView and some delegates are in charge of displaying data, do I have to use something like that?
-
Yeah, referencing memory that has been freed, that's what I'm suspecting too, and I'm trying to track down that.
But, why's that not corrupting my release version program? -
My QTimer thread is something like this:(The code is in the main GUI thread)
@
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
timer->start(1000);
@
-