Connect Thread and access public variables

0

I have created a thread in the main()-function. The thread is emitting a signal at one point. I want the signal to invoke a slot in an object created by the QMainWindow class PPI w; which is than trying to access the public object videoSocket. My thread is working perfectly but as far as I know it is not possible to connect a signal to a slot in the main()-function. But when I am trying to create the thread in the QMainWindow Class but programm crashs.

This code works (axcept for the connect):

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    PPI w;
    w.show();

    VideoUDPSocketThread videoUDPSocketThread;
    videoUDPSocketThread.start();

    //connect(&videoUDPSocketThread, SIGNAL(videoRead(int)), &w, SLOT(onVideoRead(int)), Qt::QueuedConnection);

    return a.exec();
}

This code will cause my program to crash:

PPI::PPI(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
    ui.setupUi(this);

    ppiScene = new PPIScene(this);
    ppiScene->setSceneRect(0,0,SCENEWIDTH,SCENEHEIGHT);
    ui.gVPPI->setScene(ppiScene);

    videoImage = new QImage();
    videoPixmap = ppiScene->addPixmap(QPixmap::fromImage(*videoImage));


    VideoUDPSocketThread videoUDPSocketThread;
    //videoUDPSocketThread.start(); <--CRASH

    connect(&videoUDPSocketThread.videoSocket, SIGNAL(videoRead(int)), this, SLOT(onVideoRead(int)));
}

VideoUDPSocketThread.h

#ifndef VIDEOUDPSOCKETTHREAD_H
#define VIDEOUDPSOCKETTHREAD_H

#include <QThread>
#include "videoudpsocket.h"

class VideoUDPSocketThread : public QThread
{
    Q_OBJECT

public:
    //VideoUDPSocketThread(QObject *parent);
    VideoUDPSocketThread();
    ~VideoUDPSocketThread();
    VideoUDPSocket videoSocket;

private:

protected:
    void run();
};

#endif // VIDEOUDPSOCKETTHREAD_H

VideoUDPSocketThread.cpp

#include "videoudpsocketthread.h"

/*VideoUDPSocketThread::VideoUDPSocketThread(QObject *parent)
    : QThread(parent)
{

}*/

VideoUDPSocketThread::VideoUDPSocketThread()
{

}

VideoUDPSocketThread::~VideoUDPSocketThread()
{

}

void VideoUDPSocketThread::run()
{
    VideoUDPSocket videoSocket;
    exec();
}

QThread: Destroyed while thread is still running

Eine Ausnahme (erste Chance) bei 0x5d9d5a29 in ppi.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000074.

Unbehandelte Ausnahme bei 0x5d9d5a29 in ppi.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000074.

(My try to translate this error into English:)

QThread: Destroyed while thread is still running

An exception (first chance) occured at 0x5d9d5a29 in ppi.exe: 0xC0000005: Access violation while reading at position 0x00000074.

Untreated exception at 0x5d9d5a29 in ppi.exe: 0xC0000005: Access violation while reading at position 0x00000074.

multithreading
qt
asked on Stack Overflow Oct 5, 2015 by honiahaka10

1 Answer

2

You create a Thread on the stack in PPI's constructor. This means it will be destroyed the second you function returns. I think you want something like this:

PPI::PPI(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
    ui.setupUi(this);

    ppiScene = new PPIScene(this);
    ppiScene->setSceneRect(0,0,SCENEWIDTH,SCENEHEIGHT);
    ui.gVPPI->setScene(ppiScene);

    videoImage = new QImage();
    videoPixmap = ppiScene->addPixmap(QPixmap::fromImage(*videoImage));


    VideoUDPSocketThread * videoUDPSocketThread = new VideoUDPSocketThread();

    // connect BEFORE starting to ensure thread safety (this is very important)
    // connect the finished signal to the deleteLater slot to make sure your memory doesn't leak
    // also use the new connection syntax, compiler warnings are nice
    connect( &videoUDPSocketThread->videoSocket, &VideoUDPSocket::videoRead, this, &PPI::onVideoRead );
    connect( videeoUDPSocketThread, &QThread::finished, videeoUDPSocketThread, &QObject::deleteLater ); 

    // now you can start the tread
    videeoUDPSocketThread->start();
}

I hope this helps

answered on Stack Overflow Oct 5, 2015 by Teimpz

User contributions licensed under CC BY-SA 3.0