Results 1 to 7 of 7

Thread: Connect call causes a SEGFAULT

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Connect call causes a SEGFAULT

    Hey all,

    The code compiles and links w/o issue.
    Here's where I make the connect() call in my MainWindow class. It happens in the initialisation code of my Main Window. I have verified that the source and target of signal and slot respectively are valid addresses and being created.

    This yields the segfault
    Qt Code:
    1. void CMainWindow::createSignalSlots() {
    2. connect(m_pGraphWidget, SIGNAL(createObj(QGraphicsItem *)), m_pPhysObjNavigator, SLOT(onCreateObj(QGraphicsItem *)));
    3. }
    To copy to clipboard, switch view to plain text mode 

    m_pGraphWidget is defined by the class definition like so:
    Qt Code:
    1. class GraphWidget : public QGraphicsView {
    2. Q_OBJECT
    3. signals:
    4. void createObj(QGraphicsItem *);
    5. void removeObj(QGraphicsItem *);
    6. void modifyObj(QGraphicsItem *);
    To copy to clipboard, switch view to plain text mode 

    m_pPhysObjNavigator is defined by the class definition like so:
    Qt Code:
    1. class PhysObjectNavigator : public QTreeView {
    2. public:
    3. PhysObjectNavigator(QWidget * = NULL);
    4.  
    5. public slots:
    6. void onCreateObj(QGraphicsItem *);
    To copy to clipboard, switch view to plain text mode 

    Code in GraphWidget that makes the emit call is here:
    Qt Code:
    1. void GraphWidget::createCartesianGraph() {
    2.  
    3. // Create the object
    4. m_pCartGraph = new CartesianGraph(this);
    5. m_pCartGraph -> setPos(0, 0);
    6. m_pScene -> addItem(m_pCartGraph);
    7. emit createObj(m_pCartGraph);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Code that is supposed to receive the signal is:
    Qt Code:
    1. void PhysObjectNavigator::onCreateObj(QGraphicsItem *pObj) {
    2. if (pObj) {
    3. switch (pObj -> type()) {
    4. case PhysBaseItem::VectorType:
    5. break;
    6. case PhysBaseItem::ParticleType:
    7. break;
    8. case PhysBaseItem::CartesianGraphType:
    9. break;
    10. default:
    11. break;
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    I thought it might be because PhysObjectNavigator does not have the Q_OBJECT macro defined in it but when I do put it there I get the error:
    undefined reference to 'vtable for PhysObjectNavigator'
    However it is derived from QTreeView which already includes Q_OBJECT so it shouldn't be a big deal.

    Any ideas? It all seems set up and ready to go.
    Last edited by Caolan O'Domhnaill; 24th October 2015 at 01:43. Reason: I made a lame rookie mistake -- repurposing since I cannot delete it

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect call causes a SEGFAULT

    IMO, PhysObjectNavigator must be a Q_OBJECT. It does not matter that QTreeView is. Place Q_OBJECT in the PhysObjectNavigator, delete both debug and release dirs in your project and compile from scratch. The error can result from make logic.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect call causes a SEGFAULT

    Quote Originally Posted by Caolan O'Domhnaill View Post
    I thought it might be because PhysObjectNavigator does not have the Q_OBJECT macro defined in it but when I do put it there I get the error:
    undefined reference to 'vtable for PhysObjectNavigator'
    You need to re-run qmake.

    The earlier run of qmake did not see the Q_OBJECT macro in the header for that class.
    So it did not add the rules for MOC for that header.
    So the code that MOC generates for Q_OBJECT is now missing.

    Another run of qmake will add these rules.

    Quote Originally Posted by Caolan O'Domhnaill View Post
    However it is derived from QTreeView which already includes Q_OBJECT so it shouldn't be a big deal.
    You added new slots in your subclass.
    These need to be made known to the signal/slot system (*). Q_OBJECT or rather the MOC run processing the class, does that.

    Cheers,
    _

    (*) for use with the SLOT() macro.
    Would already work with the member function pointer connect() variant introduced in Qt5

  4. The following user says thank you to anda_skoa for this useful post:

    Caolan O'Domhnaill (27th October 2015)

  5. #4
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Connect call causes a SEGFAULT

    Quote Originally Posted by anda_skoa View Post
    You need to re-run qmake.

    The earlier run of qmake did not see the Q_OBJECT macro in the header for that class.
    So it did not add the rules for MOC for that header.
    So the code that MOC generates for Q_OBJECT is now missing.

    Another run of qmake will add these rules.


    You added new slots in your subclass.
    These need to be made known to the signal/slot system (*). Q_OBJECT or rather the MOC run processing the class, does that.

    Cheers,
    _

    (*) for use with the SLOT() macro.
    Would already work with the member function pointer connect() variant introduced in Qt5

    I have added the Q_OBJECT to the View classes I am working with. I re-executed qmake and recompiled. I no longer get the error undefined reference to 'vtable for PhysObjectNavigator' on anything however I still get the SEGFAULT in the connect call.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect call causes a SEGFAULT

    Are both pointers valid?

    If you have not done so already, initialize both pointers to 0 in CMainWindow's constructor. It is easier to spot a null pointer than an invalid pointer.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    Caolan O'Domhnaill (27th October 2015)

  8. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect call causes a SEGFAULT

    More in detail:
    (1) compile for debugging.
    (2) run and see that you get the segfault.
    (3) put break on the suspected connect()
    (4) put break at the beginning of the onCreateObj() handler
    (5) debug
    (6) check the values of m_pGraphWidget and m_pPhysObjNavigator at the connect(). If you pass and get into the handler
    (7) check the value of pobj

    Post your findings

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

    Caolan O'Domhnaill (27th October 2015)

  10. #7
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Connect call causes a SEGFAULT

    I actually figured it out... What you stated for the steps is what I ended up doing (w/o) breakpoints in onCreateObj() and I made a rookie mistake: I didn't initialise my pointers in the ctor of the CMainWindow. If I had, i would have seen that my main widget was being created AFTER the CMainWindow class in main(). What clued me in to this was that the address it had was not the in the same address range that the application allocated for itself for the other objects. And when I cracked it open in the debugger none of its member pointers were valid addys as well.

    I am embarrassed by this as I should know better.

    Cheers!
    -Caolan.
    Last edited by Caolan O'Domhnaill; 27th October 2015 at 17:40.

Similar Threads

  1. Replies: 2
    Last Post: 26th August 2011, 08:51
  2. Slot function not being executed in connect
    By Leirbag89 in forum Newbie
    Replies: 7
    Last Post: 19th May 2011, 19:53
  3. Replies: 3
    Last Post: 7th April 2011, 11:09
  4. Replies: 1
    Last Post: 10th December 2010, 15:59
  5. QThread slot executed in GUI thread
    By tnyblom in forum Qt Programming
    Replies: 13
    Last Post: 25th May 2010, 07:49

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
  •  
Qt is a trademark of The Qt Company.