Results 1 to 8 of 8

Thread: Qt collections crash with SIGSEGV

  1. #1
    Join Date
    May 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Qt collections crash with SIGSEGV

    Hello!
    I'm writing a class using Qt collections classes (i.e. QVector, QMultiHash, QMultiMap). The first class I tried to use was QMultiHash. After getting crash similar to described below, I tried using QMultiMap, got same crash and now tried QVector class.

    Back to subject. The class i'm writing looks like this:
    Qt Code:
    1. //graph.h
    2. class Graph :public QObject, public QGraphicsItemGroup
    3. {
    4. Q_OBJECT
    5. QVector< QVector <Node*> > nodes;
    6. public:
    7. Graph(QObject* parent=0);
    8. ~Graph();
    9. signals:
    10. void maxLevel(int);
    11. public slots:
    12. Node* addNode(const int level=0);
    13. private slots:
    14. void removeNode(QObject *arg);
    15. private:
    16. void calculateGeometry();
    17. };
    To copy to clipboard, switch view to plain text mode 
    And the crash happens in this function.
    Qt Code:
    1. //graph.cpp
    2. Node* Graph::addNode(const int level)
    3. {
    4. Node* ret=NULL;
    5. if(level<0||level>nodes.count())return ret; //crash here
    6. if(level==nodes.count()) nodes.insert(level,QVector<Node*>());
    7. Node* node=new Node();
    8. ret=node;
    9. node->setText(trUtf8("Level %1").arg(level));
    10. connect(node,SIGNAL(destroyed(QObject*)),this,SLOT(removeNode(QObject*)));
    11. addToGroup(node);
    12. nodes[level].append(node);
    13. calculateGeometry();
    14. emit maxLevel(nodes.count());
    15. return ret;
    16. }
    To copy to clipboard, switch view to plain text mode 

    A pointer to an object of this class is used inside of a class, derived from QMainWindow.
    Being called from it's constructor it works just fine.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent):
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. //........
    6. Graph* g=new Graph();
    7. g->addNode();
    8. //........
    9. }
    To copy to clipboard, switch view to plain text mode 
    But when addNode() is called later (not inside constructor, via signal/slot) it crashes on the string I specified. In debug mode it stops on this line inside QVector:
    Qt Code:
    1. //qvector.h
    2. //........
    3. inline int count() const { return d->size; }
    4. //........
    To copy to clipboard, switch view to plain text mode 

    As I said, the same thing happened with QMultiHash and QMultiMap. I'm using Qt 4.8.1 with QtCreator 2.4.1 under Linux Mint 13 (Maya) 64 bit (kernel 3.2.0-23-generic.

    Is this an issue of my code or Qt? (I tend to think that this is my code's fault, but I can't find source of the problem. Please help! BTW, The code provided compiles without a single error.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt collections crash with SIGSEGV

    The indicated line cannot crash. The call to Vector::count() is effectively a one-liner returning an int member variable. If this were broken Qt would be fundamentally unable to start: QVector is used widely.

    Run a debug build of your program in your debugger. When it crashes copy and paste the stack backtrace and the value of the pointer you are calling add Node() through and post them here. My guess is that you are trying to access addNode() through a null or invalid pointer to Graph.

  3. The following user says thank you to ChrisW67 for this useful post:

    asmfreak (4th May 2013)

  4. #3
    Join Date
    May 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt collections crash with SIGSEGV

    the first call to addNode() fuction from MainWindow's constructor:
    Qt Code:
    1. 0 Graph::addNode graph.cpp 43 0x4100b8
    2. 1 MainWindow::MainWindow mainwindow.cpp 28 0x407894
    3. 2 main main.cpp 7 0x4074de
    To copy to clipboard, switch view to plain text mode 

    A line before crash:
    Qt Code:
    1. level 0 int
    2. node 29 @0x1d Node
    3. ret 0x0 Node *
    4. this 4271749 @0x412e85 Graph
    5. [vptr] 1142560928759121224
    6. d_ptr @0x894cfa894c00401f QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData>>
    7. [vptr] 15858580846535066870
    8. d_ptr @0x75eb394801c38348 QScopedPointer<QGraphicsItemPrivate, QScopedPointerDeleter<QGraphicsItemPrivate>>
    9. nodes <unavailable synchronous data> QVector<QVector<Node*>>
    To copy to clipboard, switch view to plain text mode 
    And crash. QtCreator shows a message with "App is stopped due to system message. SIGSEGV Sigmentation fault."
    Qt Code:
    1. 0 QVector<QVector<Node*> >::count qvector.h 259 0x41066f
    2. 1 Graph::addNode graph.cpp 33 0x40fef4
    3. 2 MainWindow::addNode mainwindow.cpp 197 0x4098db
    4. 3 MainWindow::qt_static_metacall moc_mainwindow.cpp 124 0x411f6d
    5. 4 QMetaObject::activate qobject.cpp 3547 0x7ffff6dc9281
    6. 5 QAbstractButton::clicked moc_qabstractbutton.cpp 220 0x7ffff7967c72
    7. 6 QAbstractButtonPrivate::emitClicked qabstractbutton.cpp 548 0x7ffff76a5a4e
    8. 7 QAbstractButtonPrivate::click qabstractbutton.cpp 541 0x7ffff76a6d8b
    9. 8 QAbstractButton::mouseReleaseEvent qabstractbutton.cpp 1123 0x7ffff76a6ffc
    10. 9 QWidget::event qwidget.cpp 8362 0x7ffff7328144
    11. 10 notify_helper qapplication.cpp 4559 0x7ffff72d7894
    12. 11 QApplicationPrivate::notify_helper qapplication.cpp 4531 0x7ffff72d7894
    13. 12 QApplication::notify qapplication.cpp 4102 0x7ffff72dd0bf
    14. 13 QCoreApplication::notifyInternal qcoreapplication.cpp 876 0x7ffff6db4e9c
    15. 14 sendEvent qcoreapplication.h 231 0x7ffff72d8862
    16. 15 QApplicationPrivate::sendMouseEvent qapplication.cpp 3170 0x7ffff72d8862
    17. 16 QETWidget::translateMouseEvent qapplication_x11.cpp 4617 0x7ffff7357bf5
    18. 17 QApplication::x11ProcessEvent qapplication_x11.cpp 3732 0x7ffff7356bae
    19. 18 x11EventSourceDispatch qguieventdispatcher_glib.cpp 146 0x7ffff73800d2
    20. 19 g_main_context_dispatch /lib/x86_64-linux-gnu/libglib-2.0.so.0 0 0x7ffff5750d53
    21. 20 ?? /lib/x86_64-linux-gnu/libglib-2.0.so.0 0 0x7ffff57510a0
    22. ... <Более>
    To copy to clipboard, switch view to plain text mode 
    Added this.d to watch window:
    Qt Code:
    1. this.d QVectorData
    2. alloc int
    3. capacity <unavailable synchronous data>
    4. ref <unavailable synchronous data> QBasicAtomicInt
    5. reserved <unavailable synchronous data>
    6. sharable <unavailable synchronous data>
    7. size int
    To copy to clipboard, switch view to plain text mode 
    One thing I can say for sure - I'm not doing anything with Graph* variable except calling deleteLater() on it in destructor of MainWindow.

    UPD: I tried using that QVector in other fuctions. It works cool. The only function that seem to work falsely is addNode().
    Last edited by asmfreak; 3rd May 2013 at 23:24.

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt collections crash with SIGSEGV

    How many Qt installs do you have? Are you mixing release and debug libraries? Please show compiler & linker output to confirm what you say.

    Thanks
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. The following user says thank you to amleto for this useful post:

    asmfreak (4th May 2013)

  7. #5
    Join Date
    May 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: Qt collections crash with SIGSEGV

    I guess that I need to send all of the sources.
    In this ->demo.tar.gz<- archive files:
    1) build.log - log from QtCreator after rebuild
    2) 'dpkg-query --list | grep qt4.log' - log from console from dpkg.
    everything else - sources.

    Thanks in advance.

  8. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt collections crash with SIGSEGV

    look at how you set up (or rather do not setup) MainWindow::g

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include "graph.h"
    7. #include "node.h"
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. QStateMachine *saveStates;
    17. Graph* g; ///////////
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24.  
    25. void createStates();
    26. void createDialogs();
    27. signals:
    28. void accept_file();
    29. void accept_save();
    30. // <SNIP>
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. scene=new QGraphicsScene();
    10. Graph* g=new Graph; /////////// really?
    11. g->setPos(0,0);
    12. g->getGeometry();
    13. scene->addItem(g);
    14. Node* node=new Node;
    15. node->setPos(20,20);
    16. scene->addItem(node);
    17. node=new Node;
    18. node->setPos(80,80);
    19. scene->addItem(node);
    20. node=new Node;
    21. node->setPos(180,180);
    22. scene->addItem(node);
    23. ui->graphicsView->setScene(scene);
    24. connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(setScale(int)));
    25. ui->horizontalSlider->setValue(75);
    26. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(adNode()));
    27. connect(g,SIGNAL(maxLevel(int)),this,SLOT(setMaxDial(int)));
    28. Node* test=g->addNode(0);
    29. g->addNode(0);
    30. g->addNode(1);
    31. g->addNode(2);
    32. saveStates=new QStateMachine;
    33. this->createStates();
    34. }
    35.  
    36. void MainWindow::adNode()
    37. {
    38. this->g->addNode(ui->dial->value()); ////// oops!
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 4th May 2013 at 13:37.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. The following user says thank you to amleto for this useful post:

    asmfreak (4th May 2013)

  10. #7
    Join Date
    May 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt collections crash with SIGSEGV

    Oh, what an idiot I am!
    Thank you for your help!!
    I guess this thread can be closed, since the problem is solved.

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt collections crash with SIGSEGV

    I missed it in your code too.

    I always add member variables, particularly pointers, to the class initialiser list at the same time as I add them to the declaration. If your code had read:
    Qt Code:
    1. MainWindow(QWidget *p = 0): g(0) { ... }
    To copy to clipboard, switch view to plain text mode 
    Then the invalid pointer would have been more obvious both as a 0x0 value in the debugger and, depending on compiler/toolchain, a more specific message when it crashed. Uninitialised it contains whatever garbage is in the memory and is far harder to see that it is not valid.
    Qt Code:
    1. // or this
    2. MainWindow(QWidget *p = 0): g(new Graph(this)) { ... }
    To copy to clipboard, switch view to plain text mode 
    This provides a valid initial value and would have removed the need for the initialisation that tripped you up.

Similar Threads

  1. Sigsegv
    By babygal in forum Newbie
    Replies: 3
    Last Post: 2nd November 2010, 05:29
  2. QTextEdit SIGSEGV
    By Flayer in forum Qt Programming
    Replies: 4
    Last Post: 14th January 2010, 22:52
  3. sigsegv ?
    By mero in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2009, 18:01
  4. Program crashes (SIGSEGV)
    By Voldemort in forum Qt Programming
    Replies: 47
    Last Post: 21st May 2007, 20:09
  5. qmake and image collections
    By villy in forum Qt Programming
    Replies: 5
    Last Post: 4th October 2006, 11:40

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.