July 4, 2011

huckfinn huckfinn
Lab Rat
93 posts

[solved] QHash’s insert() wrong use?—> VS-Debugger showed botch data due to missing Qt-Addon for VS

Page  
1

Hello peoplez,

I have a beginner problem with my QHash

  1. QHash<pi_int32, SpecialObject> myQHash;

I built a controller, which handles my QHash. This Controller has got a method:

  1. void DataController::addNewData(qint32 tID, QString tName, qint32 tCat, qint32 tCon)
  2. {
  3.  SpecialObject tPI (tID, tName, tCat, tCon); // tPI is set correctly
  4.  this->myQHash.insert(tID, tPI); // wrong values inserted into myQHash
  5. }

The object tPI is correctly; all its attributes are correctly set.

The error is in the next line, where the key-SpecialObject-value pair (tID, tPI) should be inserted to myQHash. It is not real error, but rather an incorrect insertion.

For testing purposes, I have elsewhere:

  1. pDataController.addNewData(2511, tr("Home"), 20481, 1272923);

But according to my debugger following values are stored
(1, BadPtr, 262148, 17) instead of
(2511, tr(“Home”), 20481, 1272923)

Does anybody know which mistake I have done? I am thankful for any advise.

Cheers Huck

44 replies

July 4, 2011

huckfinn huckfinn
Lab Rat
93 posts

Here is an screenshot of that debugger output.

As I said, Its no real error, the application does not terminate or interrupt.

July 4, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

SpecialObject goes out of scope right after you insert it into your hash. Afterwards its contents is undefined.

July 4, 2011

HuXiKa HuXiKa
Lab Rat
83 posts

You have to create your SpecialObject on the heap, because this way it gets deleted after you leave the addNewData function.

  1.  
  2. QHash<pi_int32, SpecialObject*> myQHash;

  1. void DataController::addNewData(qint32 tID, QString tName, qint32 tCat, qint32 tCon)
  2. {
  3.  SpecialObject *tPI = new SpecialObject(tID, tName, tCat, tCon);
  4.  this->myQHash.insert(tID, tPI);
  5. }

 Signature 

If you can find faults of spelling in the text above, you can keep them.

July 4, 2011

huckfinn huckfinn
Lab Rat
93 posts

Ok, and as far as I know every with new created pointer should be deleted by programmer later on.

  1. void DataController::addNewData(qint32 tID, QString tName, qint32 tCat, qint32 tCon) {  
  2.   SpecialObject *tPI = new SpecialObject(tID, tName, tCat, tCon);
  3.   this->myQHash.insert(tID, tPI);
  4.   delete tPI;
  5. }

does not make any sence right? Shall I delete all those SpecialObjects pointers in the destructor later? How would you handle that?

July 4, 2011

ludde ludde
Lab Rat
325 posts

A bit difficult to give advice with so little information. If SpecialObject is a small class, and it makes sense to pass SpecialObject objects around by value, then the code you have written should be OK. If not, then you must create the objects on the heap, using new, and then delete them when you no longer need them, e.g. when/before the QHash is destroyed.

July 4, 2011

ZapB ZapB
Ant Farmer
1427 posts

Looks to me like you have not properly implemented the assignment and/or copy constructor of your SpecialObject class properly.

@Hunger I thought QHash took a copy of objects when you insert them rather than using a reference to them as you are implying?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

July 4, 2011

huckfinn huckfinn
Lab Rat
93 posts

@ludde: Yes at that moment SpecialObject has got one QString and 5 qint32 attributes.
However, this is going to be developed and thus rising.

@ZapB: Thatswhy I wrote an own class for it, I know that I need special format for output purposes late on. And thatswhy I haven’t take “struct”.

Anyway, creating my object on the heap does not solve my problem either. During debugging I can see botch values in pDataController as on the screenshot above. And same CXX0030 error.

July 4, 2011

Volker Volker
Ant Farmer
5428 posts

ZapB wrote:
Looks to me like you have not properly implemented the assignment and/or copy constructor of your SpecialObject class properly.

@Hunger I thought QHash took a copy of objects when you insert them rather than using a reference to them as you are implying?

Correct!

To quote from the docs about container classes [doc.qt.nokia.com]
The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn’t cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget’s copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.

And more important:

If we don’t provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy.

Theses autogenerated operators and constructors are most likely not sufficient!

July 4, 2011

ZapB ZapB
Ant Farmer
1427 posts

Have you unit tested you SpecialObject class to ensure that it works properly for common operations (e.g. creation, destruction, assignment, copying, output)?

Can you post the code for SpecialObject please?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

July 4, 2011

ludde ludde
Lab Rat
325 posts

I think you will have to show us the SpecialObject class, if you cannot figure out what’s wrong with it from the help you’ve been given so far.

July 4, 2011

Volker Volker
Ant Farmer
5428 posts

Even better, create a complete test case, consisting of just the class, a main method and a method that triggers the crash.

July 5, 2011

huckfinn huckfinn
Lab Rat
93 posts

Here you have compileable but not yet linkable codes. LinkError see bottom.

DataItem.cpp

  1. #include "DataItem.h"
  2. #include <QString>
  3. DataItem::DataItem()
  4. {
  5.  this->dataID = 0;
  6.  this->dataCon = 0;
  7.  this->dataCat = 0;
  8.  this->dataName = "defName";
  9. }
  10.  
  11. DataItem::DataItem(qint32 tId, QString tName, qint32 tCat, qint32 tCon)
  12. {
  13.  this->dataID = tId;
  14.  this->dataCon = tCon;
  15.  this->dataCat = tCat;
  16.  this->dataName = tName;
  17. }
  18.  
  19. qint32 DataItem::getID() const { return this->dataID; }
  20.  
  21. QString DataItem::getName() const { return this->dataName; }
  22.  
  23. qint32 DataItem::getCat() const { return this->dataCat; }
  24.  
  25. qint32 DataItem::getCon() const { return this->dataCon; }
  26.  
  27. DataItem::~DataItem(void) {}

DataItem.h

  1. #pragma once
  2.  
  3. #include <QString>
  4.  
  5. class DataItem
  6. {
  7. public:
  8.  DataItem();
  9.  DataItem(qint32 tId, QString tName, qint32 tCat, qint32 tCon);
  10.  ~DataItem(void);
  11.  
  12.  qint32 getID() const;
  13.  QString  getName() const;
  14.  qint32 getCat() const;
  15.  qint32 getCon() const;
  16.  
  17. private:
  18.  qint32 dataID;
  19.  QString  dataName;
  20.  qint32 dataCat;
  21.  qint32 dataCon;
  22. };

DataController.cpp

  1. #include <QHash>
  2. #include "DataController.h"
  3. #include "DataItem.h"
  4.  
  5. //DataController::DataController(void)
  6. //{
  7. //  
  8. //}
  9.  
  10. //void DataController::addNew(DataItem * tPI) { this->myQHash.insert(tPI->getID(), *tPI); }
  11.  
  12. void DataController::addNew(qint32 tID, QString tName, qint32 tCat, qint32 tCon)
  13. {
  14.  //DataItem tPI (tID, tName, tCat, tCon);
  15.  DataItem * tPI = new DataItem(tID, tName, tCat, tCon);
  16.     this->myQHash.insert(tID, tPI);
  17. }
  18.  
  19. DataController::~DataController(void) {}

DataController.h

  1. #pragma once
  2.  
  3. #include <QString>
  4. #include <QHash>
  5. #include "DataItem.h"
  6.  
  7. class DataController
  8. {
  9. public:
  10.  //DataController(void);
  11.  ~DataController(void);
  12.  //void addNew(DataItem * tPI);
  13.  void addNew(qint32, QString, qint32, qint32);
  14.  
  15. private:
  16.   QHash<qint32, DataItem*> myQHash;
  17. };

MyDockWidget.cpp

  1. #include "MyDockWidget.h"
  2. #include "DataController.h"
  3. #include <QString>
  4. MyDockWidget::MyDockWidget( )
  5. {
  6.         //DataController myDataController;
  7. }
  8.  
  9. int MyDockWidget::main(int p_argsc, char *p_argsv[] )
  10. {
  11.     //this->myDataController = new DataController();
  12.     myDataController.addNew(2511, "Heim", 20481, 1272923);
  13.     myDataController.addNew(2512, "Work", 20482, 1272963);
  14.     // Breakpoint here
  15.  
  16.     return 1;
  17. }

MyDockWidget.h

  1. #ifndef MY_DOCK_WIDGET_H
  2. #define My_DOCK_WIDGET_H
  3. #include <QString>
  4. #include "DataController.h"
  5.  
  6.  
  7. class MyDockWidget
  8. {
  9.     //Q_OBJECT
  10.  
  11.     public:
  12.         /// @brief constructor
  13.         MyDockWidget();
  14.  
  15.         DataController myDataController;
  16.        
  17.   int main( int p_argsc, char *p_argsv[] );
  18. };
  19. #endif // MY_DOCK_WIDGET_H

I now get a Linker-Error:
1>Linking…
1>DataItem.obj : error LNK2019: unresolved external symbol “__declspec(dllimport) public: __thiscall QString::~QString(void)” (__imp_??1QString@@QAE@XZ) referenced in function __unwindfunclet$??0DataItem@@QAE@XZ$0

but I did not use any Libs or Dlls??! How is that be?

July 5, 2011

ZapB ZapB
Ant Farmer
1427 posts

Yes you are. You are using the QtCore dll. Also where is your main() function?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

July 5, 2011

Gerolf Gerolf
Robot Herder
3286 posts

You are missing the linker options in your pro file.
can you show it to us?
or what do you use for compiling?

For me, your code compiles fine.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

July 5, 2011

ludde ludde
Lab Rat
325 posts

Code looks OK to me. Except your DataController destructor should probably destroy the DataItems. But the way your DataItems are now, you should be able to pass them around by value, i.e. not bother with new/delete and the use of pointers.

Page  
1

  ‹‹ Compiler flags      readyRead() signal in QUdpSocket isn’t being triggered. ››

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