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. hasPendingDatagrams() generates access violation - UDPSocket
QtWS25 Last Chance

hasPendingDatagrams() generates access violation - UDPSocket

Scheduled Pinned Locked Moved Solved General and Desktop
qudbsocket
6 Posts 4 Posters 702 Views
  • 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 Offline
    T Offline
    Tamis2018
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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
        5
        • T Offline
          T Offline
          Tamis2018
          wrote on last edited by
          #4

          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_1980A 1 Reply Last reply
          0
          • T Tamis2018

            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_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #5

            @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
            4
            • T Offline
              T Offline
              Tamis2018
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

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