PDA

View Full Version : Strange QHash crash



t_3
15th December 2013, 01:04
precondition:


static QHash<SomeQObject *, QString> s_register;

SomeQObject::~SomeQObject()
{
s_register.remove(this);
}
if the deconstructor is called during program shutdown there happens a crash in the QHash detach helper (in line 10); the object is valid at the time when "remove" is called, but the key below is now apparently a null pointer...


template <class Key, class T>
struct QHashNode
{
QHashNode *next;
uint h;
Key key;
T value;

inline QHashNode(const Key &key0) : key(key0) {} // ### remove in 5.0
inline QHashNode(const Key &key0, const T &value0) : key(key0), value(value0) {}
inline bool same_key(uint h0, const Key &key0) { return h0 == h && key0 == key; }
};

this crash happens not when the deconstructor is explicitly called otherwise, it also does not happen if i use a QMap instead a QHash (what is not a big problem fortunately) - any ideas what may cause this???

wysota
16th December 2013, 14:32
Please post a minimal compilable example reproducing the problem.

t_3
16th December 2013, 20:28
Please post a minimal compilable example reproducing the problem.
thanks for taking the time to respond; i have found the reason already: the object was (accidentally) deleted on shutdown using a mutable iterator who empties the register container and in addition also deletes the contained items, thus the remove in the deconstructor was called on the already removed QHash node once again. i didn't see it in first (and 2nd and 3rd) place, as it worked (under the same conditions) with a QMap and only got evident after switching to QHash...