Qt Forum

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

    Solved hasPendingDatagrams() generates access violation - UDPSocket

    General and Desktop
    qudbsocket
    4
    6
    375
    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.
    • T
      Tamis2018 last edited by

      This is very simple code:
      …..
      bool ipChange = hostAddress.setAddress("10.7.61.100");
      if (ipChange) {
      qDebug() << hostAddress;
      }
      mainudp->bind(hostAddress,6562);
      ...
      connect(mainudp, SIGNAL(readyRead()), this, SLOT(readyReading()));
      ...
      void UDPClass::readyReading()
      {
      QByteArray buffer;
      buffer.resize(4096);
      qDebug() << "Start";
      while (mainudp->hasPendingDatagrams()) { //exception generated
      buffer.resize(mainudp->pendingDatagramSize());
      mainudp->readDatagram(buffer.data(), 4096);
      };
      The program simply generates exception when it tries to read socket data. Such as when it tries to process hasPendingDatagrams.. Firewall is turn off. Wireshark show that there are datagrams sent for the destination port and address. The address is for the second network card, installed on my PC. The data is generated and sent through own small network by a IP camera. QT is installed on Visual Studio C++.
      Exception is thrown with message:

      Exception thrown: read access violation.
      QAbstractSocket::d_func(...) returned 0xFFFFFFFFFFFFFED7

      I do not have idea what is wrong.

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

        Hi and welcome to devnet,

        Can you show the complete stack trace ?

        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
        • mrjj
          mrjj Lifetime Qt Champion last edited by mrjj

          Also, might be a silly question but
          You are 1000% sure that
          mainudp-> in UDPClass::readyReading()
          is the same as where you do
          mainudp = new UDPSocket()
          and not the classic
          UDPSocket * mainudp = new UDPSocket();
          even you already have a
          mainudp in .h and should have been
          mainudp = new UDPSocket();
          so the real mainudp is a dangling pointer.

          1 Reply Last reply Reply Quote 5
          • T
            Tamis2018 last edited by

            Thanks very much. Here is the complete listing:


            UDPClass.h

            #pragma once

            #include <QObject>
            #include<qudpsocket.h>

            class UDPClass : public QObject
            {
            Q_OBJECT

            public:
            explicit UDPClass(QObject *parent=0);
            ~UDPClass();
            void SayHello();

            signals:

            public slots:
            void readyReading();

            private:
            QUdpSocket *mainudp;
            };


            UDPClass.cpp

            #include "UDPClass.h"
            #include <Qdebug>

            UDPClass::UDPClass(QObject parent)
            : QObject(parent)
            {
            qDebug() << "Start Main";
            QUdpSocket
            mainudp = new QUdpSocket(this);
            QHostAddress hostAddress;
            bool ipChange = hostAddress.setAddress("10.7.61.100");
            if (ipChange) {
            qDebug() << hostAddress;
            }
            mainudp->bind(hostAddress,5002);

            connect(mainudp, SIGNAL(readyRead()), this, SLOT(readyReading()));
            

            }

            UDPClass::~UDPClass()
            {
            }

            void UDPClass::SayHello()
            {
            qDebug() << "Start";
            }

            void UDPClass::readyReading()
            {
            QByteArray buffer;
            buffer.resize(4096);
            //int size = mainudp->pendingDatagramSize();
            //QHostAddress sender;
            //quint16 senderPort;
            qDebug() << "Start";
            while (mainudp->hasPendingDatagrams()) {
            // qDebug() << QString("Receiving %1 bytes").arg(mainudp->pendingDatagramSize());
            buffer.resize(mainudp->pendingDatagramSize());
            mainudp->readDatagram(buffer.data(), 6562);
            //qDebug << buffer.data();

            };
            //mainudp->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
            //if ((buffer.data[0] == 0xFF) && (buffer.data[1] == 0xD8))
            //{
            //	qDebug() << "Start";
            //}
            

            }


            Main.cpp

            #include <QtCore/QCoreApplication>
            #include "UDPClass.h"

            int main(int argc, char *argv[])
            {
            QCoreApplication a(argc, argv);
            qDebug() << "Main Start";
            UDPClass serTest;
            return a.exec();
            }

            +--------------------------------------------------------------------------------

            aha_1980 1 Reply Last reply Reply Quote 0
            • aha_1980
              aha_1980 Lifetime Qt Champion @Tamis2018 last edited by aha_1980

              @Tamis2018

              so its exactly as @mrjj said: mainudp (member-variable) is not the local variable mainudp in your constructor.

              fix it!

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 4
              • T
                Tamis2018 last edited by

                That was it. Thank you all very much. I am going to put it as SOLVED.

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