Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing to the serial port crashes my application

Writing to the serial port crashes my application

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 9 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    asthana
    wrote on last edited by asthana
    #1

    Debug Trace leads me to below mentioned call
    // return write(data.constData(), data.size());
    mostly looks like the QByteArray issue. This issue happens 3 out of 5 times

    Qt Version used Qt 5.3.0 MinGW 32bit

    Any solution to this?? Thanks!

    VRoninV 1 Reply Last reply
    0
    • A asthana

      Debug Trace leads me to below mentioned call
      // return write(data.constData(), data.size());
      mostly looks like the QByteArray issue. This issue happens 3 out of 5 times

      Qt Version used Qt 5.3.0 MinGW 32bit

      Any solution to this?? Thanks!

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @asthana said in Writing to the serial port crashes my application:

      mostly looks like the QByteArray issue

      Then try return write(data); instead. No need to pass QByteArray internals

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      A 1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to the forums.
        You can try to upgrade to a newer version of Qt and see if it was fixed.

        1 Reply Last reply
        1
        • VRoninV VRonin

          @asthana said in Writing to the serial port crashes my application:

          mostly looks like the QByteArray issue

          Then try return write(data); instead. No need to pass QByteArray internals

          A Offline
          A Offline
          asthana
          wrote on last edited by
          #4

          @VRonin Hi,
          this is what the code i use
          int size = mSerialManager->controlPort->write(data);

          but on crash it goes to that line what I mentioned

          aha_1980A mrjjM 2 Replies Last reply
          0
          • A asthana

            @VRonin Hi,
            this is what the code i use
            int size = mSerialManager->controlPort->write(data);

            but on crash it goes to that line what I mentioned

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @asthana

            You need to check that mSerialManager and controlPort are valid pointers.

            Qt has to stay free or it will die.

            1 Reply Last reply
            4
            • A asthana

              @VRonin Hi,
              this is what the code i use
              int size = mSerialManager->controlPort->write(data);

              but on crash it goes to that line what I mentioned

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @asthana
              If you place a breakpoint and inspect the pointers.
              They are both valid ?
              mSerialManager / controlPort
              ?

              A 1 Reply Last reply
              3
              • mrjjM mrjj

                @asthana
                If you place a breakpoint and inspect the pointers.
                They are both valid ?
                mSerialManager / controlPort
                ?

                A Offline
                A Offline
                asthana
                wrote on last edited by
                #7

                @mrjj
                Hi,
                yeah i always check for that whether that port is null or not open
                if( mSerialManager->controlPort->isOpen())
                {
                // code
                }

                jsulmJ 1 Reply Last reply
                0
                • A asthana

                  @mrjj
                  Hi,
                  yeah i always check for that whether that port is null or not open
                  if( mSerialManager->controlPort->isOpen())
                  {
                  // code
                  }

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @asthana It's not about checking whether it is open or not. It is about checking the pointers before dereferencing them - dereferencing an invalid pointers crashes application.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    asthana
                    wrote on last edited by
                    #9

                    @jsulm
                    yeah, I check for the pointer being valid and not null by using below code , open was just another check that I recently introduced...
                    if( mSerialManager->controlPort != NULL)
                    {
                    QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                    int size = mSerialManager->controlPort->write(data);
                    }

                    jsulmJ Christian EhrlicherC 2 Replies Last reply
                    0
                    • A asthana

                      @jsulm
                      yeah, I check for the pointer being valid and not null by using below code , open was just another check that I recently introduced...
                      if( mSerialManager->controlPort != NULL)
                      {
                      QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                      int size = mSerialManager->controlPort->write(data);
                      }

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @asthana said in Writing to the serial port crashes my application:

                      int size = mSerialManager->controlPort->write(data);

                      Does it crash at this line?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        asthana
                        wrote on last edited by
                        #11

                        exactly, as per debug trace it goes to the qt internal function call i.e
                        inline qint64 write(const QByteArray &data)
                        { return write(data.constData(), data.size()); } in QIODevice.h and segmentation fault.
                        and checked the size of data as well its never zero.

                        1 Reply Last reply
                        0
                        • hskoglundH Offline
                          hskoglundH Offline
                          hskoglund
                          wrote on last edited by
                          #12

                          Hi, I'm guessing that the QByteArray is torned down before all the bytes are transmitted, you could try to use a waitForBytesWritten() before the destructor of the QByteArray:

                          if( mSerialManager->controlPort != NULL)
                          {
                              QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                              int size = mSerialManager->controlPort->write(data);
                              mSerialManager->controlPort->waitForBytesWritten(-1);
                          }
                          

                          or you could turn the QByteArray into a static one:

                          if( mSerialManager->controlPort != NULL)
                          {
                              static QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                              int size = mSerialManager->controlPort->write(data);
                          }
                          
                          A 1 Reply Last reply
                          2
                          • A asthana

                            @jsulm
                            yeah, I check for the pointer being valid and not null by using below code , open was just another check that I recently introduced...
                            if( mSerialManager->controlPort != NULL)
                            {
                            QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                            int size = mSerialManager->controlPort->write(data);
                            }

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @asthana said in Writing to the serial port crashes my application:

                            if( mSerialManager->controlPort != NULL)

                            And where do you check for mSerialManager != nullptr? Or is it guaranteed that it can never be nullptr? And is controlPort really correctly initialized and not a dangling pointer?

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            4
                            • hskoglundH hskoglund

                              Hi, I'm guessing that the QByteArray is torned down before all the bytes are transmitted, you could try to use a waitForBytesWritten() before the destructor of the QByteArray:

                              if( mSerialManager->controlPort != NULL)
                              {
                                  QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                                  int size = mSerialManager->controlPort->write(data);
                                  mSerialManager->controlPort->waitForBytesWritten(-1);
                              }
                              

                              or you could turn the QByteArray into a static one:

                              if( mSerialManager->controlPort != NULL)
                              {
                                  static QByteArray data = Datamanager::getInstance()->getItemFromOutQueueCtrl();
                                  int size = mSerialManager->controlPort->write(data);
                              }
                              
                              A Offline
                              A Offline
                              asthana
                              wrote on last edited by
                              #14

                              @hskoglund
                              "mSerialManager->controlPort->waitForBytesWritten(-1);" actually slows down the application and since I am communication it to User Interface for display , the screen hangs and don't show any activity for sometime. I also tried giving a delay through this
                              i.e "waitForBytesWritten(20)" this works fine in terms of screen updates but still it crashes.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                asthana
                                wrote on last edited by
                                #15

                                Debug Trace

                                Function: QIODevice::write(QByteArray const&)
                                0x4a3bc1 <+0x0025> mov %ebx,0x4(%esp)
                                0x4a3bc5 <+0x0029> mov %esi,0x8(%esp)
                                0x4a3bc9 <+0x002d> mov %eax,(%esp)
                                0x4a3bcc <+0x0030> mov %edx,%ecx
                                0x4a3bce <+0x0032> mov 0x544914,%eax
                                0x4a3bd3 <+0x0037> call *%eax
                                0x4a3bd5 <+0x0039> sub $0xc,%esp
                                0x4a3bd8 <+0x003c> lea -0x8(%ebp),%esp
                                0x4a3bdb <+0x003f> pop %ebx
                                0x4a3bdc <+0x0040> pop %esi
                                0x4a3bdd <+0x0041> pop %ebp
                                0x4a3bde <+0x0042> ret $0x4
                                Function: _ZN9QIODevice5writeERK10QByteArray
                                0x4a3be1 <+0x0045> nop
                                0x4a3be2 <+0x0046> nop
                                0x4a3be3 <+0x0047> nop

                                Please find the attched screenshot for the same
                                0_1554275625743_Debug Logs.png

                                aha_1980A 1 Reply Last reply
                                0
                                • A asthana

                                  Debug Trace

                                  Function: QIODevice::write(QByteArray const&)
                                  0x4a3bc1 <+0x0025> mov %ebx,0x4(%esp)
                                  0x4a3bc5 <+0x0029> mov %esi,0x8(%esp)
                                  0x4a3bc9 <+0x002d> mov %eax,(%esp)
                                  0x4a3bcc <+0x0030> mov %edx,%ecx
                                  0x4a3bce <+0x0032> mov 0x544914,%eax
                                  0x4a3bd3 <+0x0037> call *%eax
                                  0x4a3bd5 <+0x0039> sub $0xc,%esp
                                  0x4a3bd8 <+0x003c> lea -0x8(%ebp),%esp
                                  0x4a3bdb <+0x003f> pop %ebx
                                  0x4a3bdc <+0x0040> pop %esi
                                  0x4a3bdd <+0x0041> pop %ebp
                                  0x4a3bde <+0x0042> ret $0x4
                                  Function: _ZN9QIODevice5writeERK10QByteArray
                                  0x4a3be1 <+0x0045> nop
                                  0x4a3be2 <+0x0046> nop
                                  0x4a3be3 <+0x0047> nop

                                  Please find the attched screenshot for the same
                                  0_1554275625743_Debug Logs.png

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @asthana I don't think the QByteArray is the problem.

                                  Please rather check @Christian-Ehrlicher's suggestion:

                                  And where do you check for mSerialManager != nullptr? Or is it guaranteed that it can never be nullptr? And is controlPort really correctly initialized and not a dangling pointer?

                                  That seems much more a possible cause for your problem.

                                  Regards

                                  Qt has to stay free or it will die.

                                  J.HilkJ 1 Reply Last reply
                                  1
                                  • K Offline
                                    K Offline
                                    kuzulis
                                    Qt Champions 2020
                                    wrote on last edited by
                                    #17

                                    I assume that you use a multiple threads (as I can see from your screenshoot with the 'Worker' class). If so, then you do it wrong... It is my assumption.

                                    A 1 Reply Last reply
                                    1
                                    • aha_1980A aha_1980

                                      @asthana I don't think the QByteArray is the problem.

                                      Please rather check @Christian-Ehrlicher's suggestion:

                                      And where do you check for mSerialManager != nullptr? Or is it guaranteed that it can never be nullptr? And is controlPort really correctly initialized and not a dangling pointer?

                                      That seems much more a possible cause for your problem.

                                      Regards

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by J.Hilk
                                      #18

                                      @aha_1980
                                      actually it can be,
                                      from the looks of it, the serial port is threaded ( I take this from the naming of the class and the thread count)

                                      And from the looks of it, Datamanager::getInstance()->getItemFromOutQueueCtrl(); may very well become invalid during the write process. As it seams to be a singleton and may be accessed by different threads?
                                      One shouldn't do that, but it won't result in a compiler error.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      2
                                      • K kuzulis

                                        I assume that you use a multiple threads (as I can see from your screenshoot with the 'Worker' class). If so, then you do it wrong... It is my assumption.

                                        A Offline
                                        A Offline
                                        asthana
                                        wrote on last edited by
                                        #19

                                        @kuzulis
                                        Yeah, there are two threads that read and write to the port, so to check on that I stopped sending any data from User Interface(UI) that my application was reading and only write the data to the port which is sent to UI .
                                        But still the problem is there.

                                        1 Reply Last reply
                                        0
                                        • K Offline
                                          K Offline
                                          kuzulis
                                          Qt Champions 2020
                                          wrote on last edited by kuzulis
                                          #20

                                          @asthana ,

                                          Just read about the right way using the threads. You should to create the QSP instance and to call its methods only from the same thread.

                                          E.g. if your worker was created in context of thread #2, then Worker::writeToControlPort() should be called too from the context of thread #2. Check the right thread id, just use QThread::currentThreadId() in ctor of Worker and inside of Worker::writeToControlPort() to see that this ID same.

                                          PS: Read documentation, it is a main advice to you, before asking on a forum!!! It is simple...

                                          1 Reply Last reply
                                          3

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved