PDA

View Full Version : QHash initializing



trust88
3rd May 2013, 17:28
Hi all,

Is it mandatory to initialize QHash before insert ? I have a crash on insert, althought i do insert non NULL values with the right types. My hash is a data member.


QHash<QListWidgetItem* , QStringList> m_variablepaths ;

d_stranz
3rd May 2013, 18:34
Except for the copy constructor there is no such thing as "initializing". You're apparently doing something wrong when inserting, but without any code it is impossible to know.

tuli
3rd May 2013, 18:42
also, if you're using a pointer as a key, a QHash may not be the best choice.

trust88
3rd May 2013, 19:02
It falls in Qt method detach(). Is that of any help for guiding me towards solution ?

Is it important that I pass a pointer to QstringList, and not a stack QStringList ?

What is better then Qhash ? why is Qhash not a good choice ? Should I use QMap instead ?

I tried to initialize in constructor, and i tried with a QMap, with same result.

d_stranz
3rd May 2013, 19:09
It is irrelevant what you pass as the "value" part of the hash. If you pass a QStringList, the hash table will make a copy of it if needed.


It falls in Qt method detach(). Is that of any help for guiding me towards solution ?

Look, throwing out random tidbits of information isn't helping anyone help you. Unless you post some code that actually demonstrates what you are doing, we're just pulling things out of thin air.

Given some of the other questions you've posted, the errors evident in that code, and the "hint" you've given here, my guess would be that the problem is that something you did prior to inserting values into the hash table has corrupted memory, and that the crash you are seeing is a side effect and not the actual problem.

Inserting a pointer into a hash table would in no way result in a call to detach() for anything, but if memory is corrupt, the code might be jumping off into some unknown place in your program and executing whatever it finds there.


What is better then ? why is it not a good choice ?

If all you need is to associate a QStringList with a QListWidgetItem, then you could use the QListWidgetItem::data() and QListWidgetItem::setData() methods instead of building an external data structure to keep the association.

Otherwise, QHash<> is fine for this purpose, QMap<> would work too but it is less efficient since it must keep its keys sorted.

asmfreak
3rd May 2013, 21:12
I have the same problem posted here (http://www.qtcentre.org/threads/54445-Qt-collections-crash-with-SIGSEGV), but nobody seems to answer it. :(

wysota
6th May 2013, 23:31
It falls in Qt method detach(). Is that of any help for guiding me towards solution ?

Do you have more than one thread in your application that tries to access this data structure?