0

I've got a QSharedPointer to a QMultiHash. When I try to get the QMultiHash from the QSharedPointer it messes up. My thinking is that the QHash part of the QMultiHash is unaccessible.

QSharedPointer<QMultiHash<int, JB_Node*>> aNewNodesMH(new QMultiHash<int, JB_Node*>());
aNewNodesMH->insert(1, pNode);

QVector<QSharedPointer<QMultiHash<int, JB_Node*>>> nodesMHV;
nodesMHV.append(aNewNodesMH);

QSharedPointer<QMultiHash<int, JB_Node*>> aMHp = nodesMHV.value(0);
QMultiHash<int, JB_Node*> aMH = (*aMHp);  // messes up here
QHashIterator<int, JB_Node*> i(aMH);
while (i.hasNext()){
    i.next();
    ....

Any way of fixing this so that it works OR is there another way of getting a Vector of QMultiHash's?

Cheers Jeff

Jeff
  • 95
  • 9
  • Is this the actual code? Or did you split it into header and cpp? – Martin Hennings Mar 22 '19 at 15:22
  • This is a summary of the actual code in cpp. – Jeff Mar 22 '19 at 19:42
  • I've recreated your code exactly (using `struct JB_Node { int i; }`) and I don't get any errors. What I could suggest though is that you could try using a reference to your multihash instead of a copy. ie `... &aMH = (*aMHp);` – RobbieE Mar 23 '19 at 07:38
  • @RobbieE You're right, I didn't see that - that will most certainly be the reason for OP's crashes. – Martin Hennings Mar 23 '19 at 15:11
  • My bad guys. When I summarised it to simplify the problem, I combined 2 methods into one and inadvertently cut out the problem. Turns out the problem was I was treating the QSharedPointer as a pointer and wasn't passing it by reference to the other method. Hence it was still null in the original method. Once I passed by reference it was fine. My apologies. Good lesson for including code as is. @RobbieE - what is the benefit in using &aMH instead of copy- is it only time taken or is there something else. Again my apologies. – Jeff Mar 24 '19 at 06:19
  • `QMultiHash aMH = (*aMHp);` invokes the copy constructor of `QMultiHash`. In your example it's only a bit overhead, which can be prevented by a const reference. – Martin Hennings Mar 25 '19 at 11:32

0 Answers0