Results 1 to 12 of 12

Thread: Signal emit causes segmentation fault

  1. #1
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Signal emit causes segmentation fault

    I am trying to send a DDS module with the signal-slot method. My program crashes to segmentation fault when it executes the emit signal command. The part of the code where it crashes is inside the moc file and it is:

    moc_myclass.cpp:
    Qt Code:
    1. // SIGNAL 0
    2. void MyClass::MySignal(DDSModule::Message & _t1)
    3. {
    4. void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
    5. QMetaObject::activate(this, &staticMetaObject, 0, _a);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I have tried to commenting out the connect() part, but the program still crashes so I'm guessing it has something to do with the data I am trying to send. The main code is shown below.

    myclass.h:
    Qt Code:
    1. class MyClass: public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void MySignal(DDSModule::Message &message);
    7.  
    8. public:
    9. MyClass();
    10. void ddsMessageHandler(DDSModule::Message &message);
    11.  
    12. protected:
    13. virtual void run();
    14.  
    15. private:
    16. // Random attributes
    17. };
    To copy to clipboard, switch view to plain text mode 


    myclass.cpp:
    Qt Code:
    1. void MyClass::ddsMessageHandler(DDSModule::Message &message)
    2. {
    3. emit MySignal(message);
    4. }
    5.  
    6. MyClass::MyClass()
    7. {
    8. }
    To copy to clipboard, switch view to plain text mode 

    ddshandler.h:
    Qt Code:
    1. class DDSHandler : public QThread
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. DDSHandler();
    8. signals:
    9.  
    10. void DDSDataSend(MyOtherClass *newObject);
    11.  
    12. public slots:
    13.  
    14. void handleDDSData(DDSModule::Message &message);
    15.  
    16. private:
    17. void run();
    18.  
    19. MyClass *myClass_;
    20. };
    To copy to clipboard, switch view to plain text mode 


    ddshandler.cpp:
    Qt Code:
    1. DDSHandler::DDSHandler()
    2. {
    3. myClass_ = new MyClass;
    4. myClass_->start();
    5.  
    6. qRegisterMetaType<DDSModule::Message &message>("DDSModule::Message &message");
    7.  
    8. connect(myClass_, SIGNAL(DDSDataReady(DDSModule::Message &message)),
    9. this, SLOT(handleDDSData(DDSModule::Message &message)));
    10. }
    11.  
    12. void DDSHandler::handleDDSData(DDSModule::Message &message)
    13. {
    14. myOtherClass *messageInNewForm = processMessage(DDSModule::Message &message);
    15.  
    16. emit DDSDataSend(*messageInNewForm)
    17. }
    To copy to clipboard, switch view to plain text mode 


    Is there any restrictions on what type of data I can send with the emit signal? The message that comes to the ddsMessageHandler() is valid. I have also tried removing the reference markers (&) but it doesn't have any effect either.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Signal emit causes segmentation fault

    Well, to start with, your signal is declared as taking a pointer to MyOtherClass yet when you emit the signal you are giving it a copy (a referenced pointer).
    There should be at least a warning during compile time for that.
    Then, your connect statement tells the signal is taking a reference.
    So, decide which is it!
    I have tried to commenting out the connect() part, but the program still crashes
    Where is it crashing when the connect statement is commented out?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Signal emit causes segmentation fault

    Sorry, I forgot to change the signal name to this example when trying to be more abstract. The actual connect() takes the signal MySignal which uses the reference to DDS message instead of DDSDataReady which is not declared anywhere.

    The program crashes in the same function whether or not I comment out the connect().
    Last edited by Martikai; 28th August 2017 at 06:54. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,370
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal emit causes segmentation fault

    Quote Originally Posted by Martikai View Post
    Sorry, I forgot to change the signal name to this example when trying to be more abstract. The actual connect() takes the signal MySignal which uses the reference to DDS message instead of DDSDataReady which is not declared anywhere.

    The program crashes in the same function whether or not I comment out the connect().
    Please provide the backtrace and also check if this pointer is not null.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Signal emit causes segmentation fault

    I found out that it doesn't matter what i try to send with the emit. I created a whole new test signal that contains an integer which is not connected anywhere and it still causes the exact same segmentation fault.

    For some reason the message poster at this forum tells me that my backtrace includes 17 images so I can't post it as it is. I have instead included a screen shot of it.
    Screenshot_2017-08-28_11-02-39.jpg
    Last edited by Martikai; 28th August 2017 at 09:15.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,370
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal emit causes segmentation fault

    Please provide the backtrace from your debugger. The crash is probably caused by either a null pointer dereference or by destroying an object involved in a chain of signal emissions (which boils down to dereferencing an invalid pointer too).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Signal emit causes segmentation fault

    The reference is valid when I check it with the debugger. The backtrace is added to my previous message.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Signal emit causes segmentation fault

    I created a whole new test signal that contains an integer which is not connected anywhere and it still causes the exact same segmentation fault.
    The backtrace without the code helps very little.
    Please post the code that corresponds to the backtrace, and if it is not very long, post all of it, don't "meta code" it as it masks what you are doing wrong.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Signal emit causes segmentation fault

    The test signal is not connected anywhere. The crash still happens at the same function after emit which is

    Qt Code:
    1. // SIGNAL 0
    2. void LidarSub::test(int _t1)
    3. {
    4. void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
    5. QMetaObject::activate(this, &staticMetaObject, 0, _a);
    6. }
    To copy to clipboard, switch view to plain text mode 

    lidarsub.h:

    Qt Code:
    1. #ifndef LIDARSUB_H
    2. #define LIDARSUB_H
    3.  
    4. #include "observation.h"
    5. #include <QThread>
    6. #include <QObject>
    7.  
    8. #include <ace/Log_Msg.h>
    9.  
    10. #include <dds/DdsDcpsInfrastructureC.h>
    11. #include <dds/DdsDcpsPublicationC.h>
    12.  
    13. #include <dds/DCPS/Marked_Default_Qos.h>
    14. #include <dds/DCPS/Service_Participant.h>
    15. #include <dds/DCPS/WaitSet.h>
    16.  
    17. #include "dds/DCPS/StaticIncludes.h"
    18.  
    19. #include "/home/vtt/IDL/idl_lidar/lidarScanTypeSupportImpl.h"
    20.  
    21. class LidarSub: public QThread
    22. {
    23. Q_OBJECT
    24.  
    25. signals:
    26. void test(int i);
    27.  
    28. public:
    29. LidarSub();
    30. int create();
    31. void destroy();
    32. void lidarDataConverter(LidarScan::Observation newObs);
    33.  
    34. protected:
    35. virtual void run();
    36.  
    37. private:
    38. DDS::DomainParticipantFactory_var dpf_;
    39. DDS::DomainParticipant_var participant_;
    40. DDS::DataReader_var reader_;
    41. LidarScan::ObservationDataReader_var reader_i_;
    42.  
    43. int numOfRecObs_;
    44.  
    45. };
    46. #endif // LIDARSUB_H
    To copy to clipboard, switch view to plain text mode 


    lidarsub.cpp:

    Qt Code:
    1. #include "lidarsub.h"
    2.  
    3. #include <QDebug>
    4. #include <QThread>
    5.  
    6. #include <ace/Log_Msg.h>
    7.  
    8. #include <dds/DdsDcpsInfrastructureC.h>
    9. #include <dds/DdsDcpsPublicationC.h>
    10.  
    11. #include <dds/DCPS/Marked_Default_Qos.h>
    12. #include <dds/DCPS/Service_Participant.h>
    13. #include <dds/DCPS/WaitSet.h>
    14.  
    15. #include "dds/DCPS/StaticIncludes.h"
    16.  
    17. #include "/home/vtt/IDL/idl_lidar/lidarScanTypeSupportImpl.h"
    18. #include "/home/vtt/IDL/idl_lidar/DataReaderListenerImpl.h"
    19.  
    20.  
    21. using namespace DDS;
    22. using namespace std;
    23.  
    24. void LidarSub::lidarDataConverter(LidarScan::Observation newObs)
    25. {
    26. int i = 0;
    27. emit test(i); // <- This causes the segmentation fault
    28. }
    29.  
    30. LidarSub::LidarSub()
    31. {
    32. numOfRecObs_ = 0;
    33. }
    34.  
    35. int LidarSub::create()
    36. {
    37. // OpenDDS related code that initiates the DDS configuration. Can't publish this part of the code on this forum.
    38. // This should not affect anything else than the DDS communication.
    39. }
    40.  
    41.  
    42. void LidarSub::destroy()
    43. {
    44. TheServiceParticipant->shutdown();
    45. }
    46.  
    47. void LidarSub::run()
    48. {
    49. while (true) {
    50.  
    51. QThread::msleep(10);
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 


    DataReaderListenerImpl.cpp - function that is called when new data arrives through DDS
    handle_ is pointer to the instantiation of LidarSub class

    Qt Code:
    1. void DataReaderListenerImpl::on_data_available(DDS::DataReader_ptr reader)
    2. {
    3. LidarScan::ObservationDataReader_var reader_i =
    4. LidarScan::ObservationDataReader::_narrow(reader);
    5.  
    6. if ( !reader_i )
    7. {
    8. ACE_ERROR((LM_ERROR,
    9. ACE_TEXT("ERROR: %N:%l: on_data_available() -")
    10. ACE_TEXT(" _narrow failed!\n")));
    11. ACE_OS::exit(-1);
    12. }
    13.  
    14.  
    15. LidarScan::Observation newObs;
    16. DDS::SampleInfo info;
    17.  
    18. DDS::ReturnCode_t error = reader_i->take_next_sample(newObs, info);
    19.  
    20. if (error == DDS::RETCODE_OK)
    21. {
    22.  
    23. if ( info.valid_data )
    24. {
    25. if ( handle_ != NULL )
    26. {
    27. handle_->lidarDataConverter(newObs);
    28. }
    29. }
    30.  
    31. } else
    32. {
    33. ACE_ERROR((LM_ERROR,
    34. ACE_TEXT("ERROR: %N:%l: on_data_available() -")
    35. ACE_TEXT(" take_next_sample failed!\n")));
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,370
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal emit causes segmentation fault

    Does handle_ point to a valid object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Aug 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Signal emit causes segmentation fault

    Quote Originally Posted by wysota View Post
    Does handle_ point to a valid object?
    No, thank you! I had forgotten about setting the actual pointer. For some reason the DataReaderListenerImpl is able to call the lidarDataConverter() even though the pointer is not a valid object.
    Last edited by Martikai; 28th August 2017 at 11:59.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,370
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal emit causes segmentation fault

    It's a non-virtual function so it doesn't need a proper object to call it. But when you try accessing any data, you end up with reading garbage.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Seems that my emit doesn't emit the signal
    By roseicollis in forum Newbie
    Replies: 2
    Last Post: 19th January 2015, 16:05
  2. Please help!! Segmentation fault in Qt
    By jmalicke in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2014, 05:35
  3. a segmentation fault
    By yaohao@qtcentre in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2011, 09:01
  4. Segmentation Fault
    By freekill in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2010, 15:31
  5. Segmentation Fault
    By Krish_ng in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2007, 10: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.