UDPSocket Stopped Working



  • 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());
    
        }
    
    }
    

  • Qt Champions 2016

    hi
    do you have

    uSocket = new QUdpSocket(this) ;

    anywhere?



  • @mrjj
    hi
    no! i dont this
    why??


  • Lifetime Qt Champion

    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.



  • 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.


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.