Results 1 to 11 of 11

Thread: Serializing a QHash leads to SEGFault

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Serializing a QHash leads to SEGFault

    I have a problem with a program I am working on where serializing a QHash gets me a segfault. As the project is too much code to show, I isolated what created the problem and could reproduce the segfault.

    Does anyone have a clue what I am doing wrong here? I am sorry that it's a lot of code, it's not very complex though (I am newbie at both C++ and Qt).
    It runs until the statement "data2 >> datum2". After that I get a segfault.

    Qt Code:
    1. 0000 Program start.
    2. 0001 Datum1 written to buffer.
    3. 0002 Buffer about to open for reading.
    4. 0003 data2 about to initialize with buffer.
    5. 0004 data2 initialized with buffer.
    6. 0005 data2 about to read buffer into datum2.
    7. Segmentation fault (core dumped)
    To copy to clipboard, switch view to plain text mode 

    This code is main.cpp.

    Qt Code:
    1. #include <memory>
    2. #include <iostream>
    3. #include <exception>
    4.  
    5. #include <QCoreApplication>
    6. #include <QBuffer>
    7. #include <QIODevice>
    8.  
    9. #include "datum.h"
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QCoreApplication a(argc, argv);
    14.  
    15. std::cout << "0000 Program start." << std::endl;
    16.  
    17. std::shared_ptr<Datum> datum = std::make_shared<Datum>();
    18. std::shared_ptr<Thing> thing = std::make_shared<Thing>();
    19.  
    20. thing->name("name of thing");
    21. datum->add(thing);
    22.  
    23.  
    24. QBuffer buffer;
    25.  
    26. // Write datum into buffer
    27. buffer.open(QIODevice::WriteOnly);
    28. QDataStream data(&buffer);
    29. data << datum;
    30. buffer.close();
    31. std::cout << "0001 Datum1 written to buffer." << std::endl;
    32.  
    33. // Read datum2 from buffer
    34. std::shared_ptr<Datum> datum2;
    35. std::cout << "0002 Buffer about to open for reading." << std::endl;
    36. buffer.open(QIODevice::ReadOnly);
    37. std::cout << "0003 data2 about to initialize with buffer." << std::endl;
    38. QDataStream data2(&buffer);
    39. std::cout << "0004 data2 initialized with buffer." << std::endl;
    40.  
    41. try {
    42. std::cout << "0005 data2 about to read buffer into datum2." << std::endl;
    43. data2 >> datum2;
    44. std::cout << "0006 data2 just read buffer into datum2." << std::endl;
    45. buffer.close();
    46. std::cout << "0007 Datum2 read from buffer." << std::endl;
    47. } catch (std::exception &e) {
    48. std::cout << "0008 Error during read from buffer: " << e.what() << std::endl;
    49. }
    50.  
    51. std::cout << "0009 Program end." << std::endl;
    52.  
    53. return a.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    Following the datum.h and datum.cpp files.

    Qt Code:
    1. #ifndef DATUM_H
    2. #define DATUM_H
    3.  
    4. #include <iostream>
    5. #include <memory>
    6.  
    7. #include <QDataStream>
    8. #include <QHash>
    9.  
    10. class Thing {
    11.  
    12. friend QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Thing>);
    13. friend QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Thing>);
    14.  
    15. public:
    16.  
    17. QString name() const { return m_name; }
    18. void name(const QString name) { m_name = name; }
    19.  
    20. private:
    21. QString m_name;
    22. };
    23.  
    24. class Datum
    25. {
    26.  
    27. friend QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Datum>);
    28. friend QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Datum>);
    29.  
    30. public:
    31. Datum();
    32. ~Datum();
    33.  
    34. void add(std::shared_ptr<Thing>);
    35.  
    36. private:
    37. QHash<QString, std::shared_ptr<Thing>> m_list;
    38. };
    39.  
    40. QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Thing>);
    41. QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Thing>);
    42.  
    43. QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Datum>);
    44. QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Datum>);
    45.  
    46. #endif // DATUM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "datum.h"
    2.  
    3. Datum::Datum()
    4. {
    5.  
    6. }
    7.  
    8. Datum::~Datum()
    9. {
    10.  
    11. }
    12.  
    13. void Datum::add(std::shared_ptr<Thing> thing) {
    14. m_list.insert(thing->name(), thing);
    15. }
    16.  
    17.  
    18. QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Thing> thing) {
    19. ds << thing->m_name;
    20. return ds;
    21. }
    22.  
    23. QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Thing> thing) {
    24. ds >> thing->m_name;
    25. return ds;
    26. }
    27.  
    28. QDataStream &operator<< (QDataStream &ds, std::shared_ptr<const Datum> datum) {
    29. ds << datum->m_list;
    30. return ds;
    31. }
    32.  
    33. QDataStream &operator>> (QDataStream &ds, std::shared_ptr<Datum> datum) {
    34. ds >> datum->m_list;
    35. return ds;
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by gbonnema; 6th January 2015 at 12:06. Reason: correcting the title

Similar Threads

  1. Serializing QHash to a QByteArray
    By PeterThePuter in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2010, 23:19
  2. application never quits
    By harmodrew in forum Newbie
    Replies: 2
    Last Post: 6th August 2010, 00:56
  3. Qt 4 quits application [solved]
    By osiris81 in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2008, 14:07
  4. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17
  5. hide() quits?
    By Morea in forum Newbie
    Replies: 13
    Last Post: 25th February 2006, 19:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.