September 26, 2011

leonidwang leonidwang
Lab Rat
24 posts

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

  1. template <class Key, class T>
  2. Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey,
  3.                                                                             uint *ahp) const
  4. {
  5.     Node **node;
  6.     uint h = qHash(akey);
  7.  
  8.     if (d->numBuckets) {
  9.         node = reinterpret_cast<Node **>(&d->buckets[h % d->numBuckets]);
  10.        Q_ASSERT(*node == e || (*node)->next);    //line 884
  11.         while (*node != e && !(*node)->same_key(h, akey))
  12.             node = &(*node)->next;
  13.     } else {
  14.         node = const_cast<Node **>(reinterpret_cast<const Node * const *>(&e));
  15.     }
  16.     if (ahp)
  17.         *ahp = h;
  18.     return node;
  19. }

QHash<QString, QVariant>::value in line 618 of qhash.h,

  1. template <class Key, class T>
  2. Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey, const T &adefaultValue) const
  3. {
  4.     Node *node;
  5.     if (d->size == 0 || (node = *findNode(akey)) == e) {        //line 618
  6.         return adefaultValue;
  7.     } else {
  8.         return node->value;
  9.     }
  10. }

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.

5 replies

September 26, 2011

Andre Andre
Robot Herder
6417 posts

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?

September 26, 2011

gri gri
Lab Rat
45 posts

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 ;)

September 26, 2011

peppe peppe
Ant Farmer
1029 posts
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.

A QTimer thread? Are you using threads for timers [developer.qt.nokia.com] ?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

September 27, 2011

leonidwang leonidwang
Lab Rat
24 posts

OK, here’s some more information about my program.
1. 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?

2. 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?

3. My QTimer thread is something like this:(The code is in the main GUI thread)

  1. timer = new QTimer(this);
  2. connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
  3. timer->start(1000);

September 27, 2011

Andre Andre
Robot Herder
6417 posts

If the above code is really in your GUI thread, and this is your timer, than forget everything about mutexes and the likes. You are not using threading, so that is not the solution.

Look for your solution around your point 2.

 
  ‹‹ [SOLVED]Easiest way to set image to NOT auto repeat ?      QListwidget item with mousedoubleclicked ››

You must log in to post a reply. Not a member yet? Register here!