Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved UDPSocket Stopped Working

    General and Desktop
    network udp error
    3
    6
    1529
    Loading More Posts
    • 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.
    • M4RZB4Ni
      M4RZB4Ni last edited by

      Hello
      I Want To Save state of a combobox in Char variable and send The Amount of this variable
      by UdpSocket to Client and when Client Received Amount of variable Disable or Enable Some Objects
      I Wrote a Code For Doing This But When i Run This Code Show me Stopped Working Messsage!
      SomeBody Can Help me Please?
      .h file

          QUdpSocket *uSocket;
      
      

      .cpp file

      QSettings priSettings("Mobtakeran Fanavri KabooK","Kabook Physiothrapy");
      management_menu::management_menu(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::management_menu)
      {
       ui->setupUi(this);
      primessionOfNewUser();
      
      }
      
      void management_menu::primessionOfNewUser()
      {
          char priChkNuser;
          QByteArray *priPackNuser;
          if(ui->chkNewUser->currentIndex()==0)
          {
             priChkNuser='y';
             priPackNuser->append(priChkNuser);
             uSocket->writeDatagram(priPackNuser->data(),priPackNuser->size(),QHostAddress::Broadcast,45454);
             priSettings.setValue("PNU",ui->chkNewUser->currentIndex());
          }else if(ui->chkNewUser->currentIndex()==1){
              priChkNuser='n';
              priPackNuser->append(priChkNuser);
              uSocket->writeDatagram(priPackNuser->data(),priPackNuser->size(),QHostAddress::Broadcast,45454);
              priSettings.setValue("PNU",ui->chkNewUser->currentIndex());
      
          }
      
      }
      

      Thanks
      M4RZB4Ni

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        hi
        do you have

        uSocket = new QUdpSocket(this) ;

        anywhere?

        M4RZB4Ni 1 Reply Last reply Reply Quote 2
        • M4RZB4Ni
          M4RZB4Ni @mrjj last edited by

          @mrjj
          hi
          no! i dont this
          why??

          Thanks
          M4RZB4Ni

          mrjj 1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            You're not allocating priPackNuser so you're using an invalid pointer.

            In any case, there's no need to allocate priPackNuser on the heap. You should keep it on the stack.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 2
            • M4RZB4Ni
              M4RZB4Ni last edited by

              hi
              thank you so much
              can you
              explain more and
              Correction my code please?

              i have abut 10 more function like this and if you correction this I would be grateful.

              Thanks
              M4RZB4Ni

              1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @M4RZB4Ni last edited by mrjj

                @M4RZB4Ni said:

                Hi
                When you dont have
                uSocket = new QUdpSocket(this) ;
                then
                uSocket->writeDatagram(xx
                will crash.
                as uSocket just point to random location. (dangling pointer)

                Next:
                QByteArray *priPackNuser; <<< also just a pointer.
                You should do ( as @SGaist mention)
                QByteArray priPackNuser;
                ( no *, means its real bytearray, not a pointer to one)
                So you change it from heap allocation to stack allocation.

                If you dont change to non pointer then you need
                QByteArray *priPackNuser = new QByteArray ;
                as else its a dangling pointer too!
                But no need for pointer to QByteArray here.

                1 Reply Last reply Reply Quote 3
                • First post
                  Last post