hasPendingDatagrams() generates access violation - UDPSocket
-
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 0xFFFFFFFFFFFFFED7I do not have idea what is wrong.
-
Hi and welcome to devnet,
Can you show the complete stack trace ?
-
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. -
Thanks very much. Here is the complete listing:
UDPClass.h
#pragma once
#include <QObject>
#include<qudpsocket.h>class UDPClass : public QObject
{
Q_OBJECTpublic:
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();
}+--------------------------------------------------------------------------------
-
Thanks very much. Here is the complete listing:
UDPClass.h
#pragma once
#include <QObject>
#include<qudpsocket.h>class UDPClass : public QObject
{
Q_OBJECTpublic:
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();
}+--------------------------------------------------------------------------------
so its exactly as @mrjj said: mainudp (member-variable) is not the local variable mainudp in your constructor.
fix it!