Qt Cannot send events to objects owned by a different thread - c++

I have mainwindow class with such slots in it:
void MainWindow::connect_to_server(const std::string& nickname,
const std::string& ip,
int port)
{
remote_server = new Server(nickname, ip, port);
connect(remote_server, SIGNAL(error()), SLOT(connection_failed()));
auto thread = new QThread(this);
remote_server->moveToThread(thread);
connect(thread, SIGNAL(started()), remote_server, SLOT(establish_connection()));
connect(remote_server, SIGNAL(stop_thread()), thread, SLOT(quit()));
thread->start();
}
void MainWindow::action_disconnect_triggered() {
if (remote_server == nullptr) {
return;
}
remote_server->disconnect();
remote_server = nullptr;
}
And Server class:
void Server::establish_connection() {
master_socket = std::move(
std::unique_ptr<QTcpSocket>(new QTcpSocket(nullptr))
);
master_socket->connectToHost(ip.c_str(), port);
master_socket->waitForConnected(timeout*1000);
if (master_socket->state() == QAbstractSocket::UnconnectedState) {
disconnect();
emit error();
}
emit stop_thread();
}
void Server::disconnect() {
if (master_socket) {
master_socket->disconnectFromHost();
}
}
Initially, I invoke MainWindow::connect_to_server where client successfully connected to remote server. Then, I invoke MainWindow::action_disconnect_triggered and on this stage I get such error:
Btw, when I run it in OS X 10.11, error does not emerge and all works properly. What am I doing wrong and how can I fix it?

remote_server->disconnect(); might be the issue here.
you do not send the event directly, but you call the function and it gets invoked in your main thread.
try QMetaObject::invokeMethod(remote_server, "disconnect", Qt::QueuedConnection); to see if this problem still exists
cheers

You are connecting the objects before moving to thread. That way Qt cant find it in the new one. Just move to thrrqd before everything and this should work.

Related

Qt signal slots and memory alocation

I have an application which communicates with an external application and this controls a devices. When new data have arrived in this device, my application should send an "answer" to this device. The communication with the external application is done with some callback functions and I use signals/slot to deliver the correct message to the callback.
My problem is that after the original signal returns, the application crashes with SIGSEGV error. As far as I know this error refers to failure in memory. As the external application could dealocate /destroy/corrupt the original data, could you please tell me if this a safe way to use signal/slot mechanism? Or this could have caused a SIGSEGV? Bellow, I am posting a code snippet as example:
MyClass::MyClass()
{
notifierData = new QWinEventNotifier(HandleData);
objectItem = new ObjectItem();
connect(notifierData,SIGNAL(activated(HANDLE)),SLOT(newDataSlot(HANDLE)));
QObject::connect(this, SIGNAL(objectData(int, QByteArray)), objectItem, SLOT(dataChangedSlot(int, QByteArray)), Qt::QueuedConnection) ;
}
void MyClass::newDataSlot(HANDLE)
{
DataType data;
GetDataFromExternalCallback(&data);
QByteArray bytearrayData(data.str);
emit objectData(data.TC_Index, bytearrayData);
}
void ObjectItem::dataChangedSlot(int index, QByteArray data)
{
emit dataChanged(index, data);
}
DeviceInterface::DeviceInterface()
{
Init(Line1);
}
void SetObjectItem(ObjectItem *anObjectItem)
{
objectItem = anObjectItem;
if(objectItem != NULL)
connect(objectItem, SIGNAL(dataChanged(int, QByteArray)), this, SLOT(newDataRecevied(int, QByteArray)), Qt::QueuedConnection);
}
void DeviceInterface::newDataRecevied(int index, QByteArray data)
{
QString messageToDisplay = "Reply Answer";
if(objectItem != NULL)
{
objectItem->updateDeviceDisplay(index, messageToDisplay);
}
}
bool DeviceInterface::updateDeviceDisplay(int index, QString text)
{
//Line1 is class member: char Line1[20]
sprintf_s(Line1, 20, "%s", UnicodeToCodePage(1253,text.toStdWString().c_str()));
SendDataWithExternalCallback(&Line1);
return true;
}

QThread stuck in second run

I have a QThread on my embedded device. every time I run the application my thread stuck after second run. I try to kill my thread after first run. Still device stuck after second run. I couldn't run my thread correctly.
Here is my code;
void ThreadCurrency::run()
{
QMutex mutex;
mutex.lock();
if(this->CurrencyStop == true)
{
mutex.unlock();
return;
}
QByteArray strdata;
// Create QProcess object
processCurrency = new QProcess();
processCurrency->start("curl --insecure -v --cacert /data/ca/cert.pem https://secure.*******************/fx.jsp");
if (processCurrency->waitForStarted(-1))
{
while(processCurrency->waitForReadyRead(-1))
{
strdata += processCurrency->readAllStandardOutput();
}
QMessageBox msgBox1;
msgBox1.setWindowTitle("eCode Read");
msgBox1.setText(strdata);
msgBox1.exec();
}
else
{
while(processCurrency->waitForReadyRead(-1))
{
strdata += processCurrency->readAllStandardError();
}
QMessageBox msgBox1;
msgBox1.setWindowTitle("eCode Error");
msgBox1.setText(strdata);
msgBox1.exec();
}
mutex.unlock();
sleep(1);
//*****************************************************************
emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD);
}
** The Output shows in a json format:**
{
"date": "20171107", "currency": {
"dolar": {
"buy": "3,8200",
"sale": "3,9050",
"e_sale": "3,8200"
},
}
}
Thank you for suggestion. The QMutexLocker can’t work in my embedded device. QMutex and the QThread is the closes I can get in my device.
My problem is the below line: emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD); The line runs but It doesn’t fire onCurrencyChanged SLOT. Anything in onCurrencyChanged doesn’t run. My main thread code is:
currencyThread = new ThreadCurrency (this);
connect(currencyThread,SIGNAL(CurrencyChanged(QString, QString, QString, QString, QString, QString)), this, SLOT(onCurrencyChanged (QString, QString, QString, QString, QString, QString)));
currencyThread->CurrencyStop = false;
currencyTimer = new QTimer(this);
connect(currencyTimer, SIGNAL(timeout()),this, SLOT(showCurrencyStatus()));
currencyTimer->start(30000);
void MainWindow::onCurrencyChanged(QString aGBP, QString aEUR, QString aUSD, QString sGBP, QString sEUR, QString sUSD)
{
// SHOW Currency
ui->lblALIS_STG->setText(aGBP);
ui->lblALIS_EUR->setText(aEUR);
ui->lblALIS_USD->setText(aUSD);
QCoreApplication::processEvents();
}
1) You are not allowed to access Widgets in a thread besides the main thread.
Thus move all QMessageBox code to the main thread (e.g. after receiving the CurrencyChanged signal).
2) Make sure, that the CurrencyChanged signal is connected using a QueuedConnection, otherwise, your GUI will be executed inside the secondary worker thread.
3) Do not use QMutex directly, use a QMutexLocker instead
4) Why do you need the Mutex anyhow? Locking based on (arbitrary) input thus arbitrary time is close to deadlocking your application.

QTCPSocket start by timer at the same time

I have problem with synchronize(start at the same time QTCPSocket) in my application I have 10 sockets. I have to read data at the similar time to all sockets. At this moment I have something that:
///...///
if(!socket->waitForConnected(-1))
{
qDebug() << "Server not found";
emit serverNotFound();
}else if(socket->state()==QAbstractSocket::ConnectedState){
qDebug() << "Connected"
connect(timer, SIGNAL(timeout()),this,SLOT(connected()));
timer->start(1000);
}
}
On connected signal:
void SocketsClass::connected()
{
sendRequest(socket, messageToServer);
}
The problem is that when the first socket get connected the timer starts for the one.
You can invert your approach. Don't wait for the sockets to get connected. Instead, check if the socket is connected in the slot activated by the timer. In that slot, you can iterate over all sockets and send the message to each of them.
Finally, you should never use Qt's waitForXxx methods, they lead to a horrible pseudo-synchronous code that is very error prone and hard to extend and maintain. Use the signal-slot mechanism instead.
Example:
SocketManager : public QObject {
Q_OBJECT
QTcpSocket m_sockets[10];
QTimer m_timer;
public:
SocketManager(QObject * parent = 0) : QObject(parent) {
... // connect all sockets here
m_timer.start(1000);
connect(&m_timer, &QTimer::timeout, [this]{
for (auto & socket : m_sockets)
if (socket.state() == QAbstractSocket::ConnectedState)
sendRequest(socket, messageToServer);
});
}
};

Qt QTcpSocket() readReady Signal never fires (slot never called) in multithreaded Server applicaton. waitForReadyRead() method works fine

I'm writing a threaded TcpServer (each client in its own thread) using QTcpServer and QTcpSocket. The client application works correctly and sends data every 3 seconds but the readReady() signal never fires, meaning my receive_data() function is never called. When using socket->waitForReadyRead() and calling receive_data() by myself everything works fine. Please have a look at the code below, maybe I made some mistake with the moveToThread / connect functionality Qt offers.
Client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QThread>
#include <QTcpSocket>
#include <QHostAddress>
#include "PacketDefinitions.h"
#include "tcpserver.h"
class Client : public QObject
{
Q_OBJECT
public:
explicit Client(int socket,TcpServer *parent,bool auto_disconnect = true);
~Client();
bool isGameServer(){return is_gameserver;}
GameServerPacket getGameServerData(){return gameserver;}
void run();
private:
QTcpSocket* client;
TcpServer *parent_server;
int socket;
GameServerPacket gameserver;
ClientPacket clientdata;
bool is_gameserver;
bool auto_disconnect;
QHostAddress client_ip;
quint16 client_port;
signals:
void disconnected(Client *);
private slots:
void remove_from_clientlist();
void receive_data();
void display_error(QAbstractSocket::SocketError error);
};
#endif // CLIENT_H
Client.cpp
#include "client.h"
#include "PacketDefinitions.h"
#include "time.h"
#include <iostream>
Client::Client(int _socket, TcpServer *parent,bool _auto_disconnect)
{
auto_disconnect = _auto_disconnect;
parent_server = parent;
is_gameserver = false;
socket = _socket;
}
void Client::run(){
client = new QTcpSocket();
if(client->setSocketDescriptor(socket) == false){
std::cout << client->errorString().toStdString() << std::endl;
remove_from_clientlist();
return;
}
connect(client,SIGNAL(disconnected()),this,SLOT(remove_from_clientlist()));
if(connect(client,SIGNAL(readyRead()),this,SLOT(receive_data()),Qt::DirectConnection) == false) return;
connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(display_error(QAbstractSocket::SocketError)));
client_ip = client->peerAddress();
client_port = client->peerPort();
std::cout << "New incomming connection " << client->peerAddress().toString().toStdString() << ":" << client->peerPort() << std::endl;
//this works fine
// while(client->waitForReadyRead()){
// receive_data();
// }
}
void Client::receive_data(){
QDataStream stream(client);
stream.setVersion(QDataStream::Qt_5_2);
quint32 magic; stream >> magic;
//interpret data
if(magic == GAMESERVER_MAGIC){
is_gameserver = true;
gameserver.Read(stream);
gameserver.port = client_port;
gameserver.ip = client_ip;
time(&(gameserver.last_update));
parent_server->add_server(gameserver.ip.toString(),gameserver);
std::cout << "GameServer " << gameserver.name << " registerd" << std::endl;
}else if(magic == CLIENT_MAGIC){
is_gameserver = false;
clientdata.Read(stream);
//get nearby servers
GameServerListPacket server_list = parent_server->getServerList(clientdata);
QDataStream outstream(client);
server_list.Write(outstream);
std::cout << "Sending ServerList(" << server_list.server_count << ") to " << client->peerAddress().toString().toStdString() << std::endl;
if(auto_disconnect){
//client->flush();
client->waitForBytesWritten();
}
}else{
std::cout << "Unknown package " << magic << std::endl;
}
//not enough data read, somthing is wrong, just for debugging
if(client->bytesAvailable()> 0) std::cout << "BytesAvailable " << client->bytesAvailable() << std::endl;
if(auto_disconnect) remove_from_clientlist();//close the connection once the serverlist was deployed
}
In the TcpServer.cpp add_client() is called when newConnection() was emitted by the QTcpServer:
void TcpServer::add_client(){
while(server->hasPendingConnections()){
QTcpSocket *socket = 0;
if(thread_pool.size() < max_connections && (socket = server->nextPendingConnection())){
QThread *thread = new QThread();
Client * client = new Client(socket->socketDescriptor(),this,auto_disconnect);
client->moveToThread(thread);
client->run();
thread->start();
connect(client,SIGNAL(disconnected(Client*)),this,SLOT(remove_client(Client*)));
WRITELOCK(thread_pool.insert(client,thread));
}
}
}
the order calling client->run() and thread->start() doesn't seem to matter. Some time ago the code (not this exact code) worked fine but I can't remember what I changed that made it fail. Any help is appreciated!
Thanks in advance
Fabian
Edit 1:
I derived from QTcpServer and reimplemented void incomingConnection(qintptr socketDescriptor) which works fine. I dont use QThreadPool, its just a QMap and remove_client(Client*) closes the QTcpSocket and stops the thread and removes it from the map. Everything works fine on linux, on windows I get the following error: QSocketNotifier: socket notifiers cannot be disabled from another thread ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread....
Caused by this remove_client(Client*)
void TcpServer::remove_client(Client *client){
//disconnect(client,SIGNAL(disconnected(Client*)),this,SLOT(remove_client(Client*)));
lock.lockForWrite();
QMap<Client*,QThread*>::iterator itr = thread_pool.find(client);
if(itr != thread_pool.end()){
//delete itr.key(); causes the problem on windows
itr.value()->quit();
itr.value()->wait();
delete itr.value();
thread_pool.erase(itr);
}
lock.unlock();
}
Where and how should I free the Client object? If i'd use QThreadPool theres no way to iterate through the clients in case i want to send a message to more than one client. I could use a list/map holding only the Client* but then QThreadPool might delete them for me right before i want to access it. Any suggestions?
There is a problem with how you move your client object to a new thread. Actually, Client::run executes in the same thread as TcpServer::add_client.
Also QTcpSocket client remains in the default thread, while its container (Client class) is moved to a new thread. That's why the connection with Qt::DirectConnection type doesn't work.
Try this:
class Client : public QObject
{
Q_OBJECT
...
public slots:
void run();
...
}
Client::Client(int _socket, TcpServer *parent,bool _auto_disconnect)
{
...
client = new QTcpSocket(this);
}
void Client::run()
{
...
connect(client, SIGNAL(readyRead()), this, SLOT(receive_data()));
...
}
And here's how you should move your client to a new thread:
void TcpServer::add_client()
{
...
QThread *thread = new QThread();
Client * client = new Client(socket->socketDescriptor(),this,auto_disconnect);
client->moveToThread(thread);
connect(thread, SIGNAL(started()), client, SLOT(run()));
thread->start();
...
}
There are a number of things wrong with your code.
1.You have two QTcpSocket object trying to collect data from the same underlying socket descriptor. You appear to use the first one only to get access to the socket descriptor value which you then pass to your Client class. You might end up losing data because you won't be able to tell which socket will be getting what data from the operating system.
If you are creating a derived class of QTcpServer, rather reimplement QTcpServer::incomingConnection(qintptr socketDescriptor) instead of your existing TcpServer::add_client() function. Since this protected function is called once for every new connection, you don't need to make any connections to the newConnection() signal, nor do you have to loop while new connections are pending. You will also then only have one QTcpSocket connected to each socket descriptor so you won't lose data.
2.You seem to be using QThreadPool to manage threads. If you make Client a derived class of QRunnable (take not that with multiple inheritance of QObject, QObject must always be first), you don't need to check the maximum connections and you can eliminate all the QThread boiler-plating.
Taking 1. and 2. into account, your TcpServer::add_client() function will be replaced with:
void TcpServer::incomingConnection(qintptr socketDescriptor){
Client * client = new Client(socketDescriptor,this,auto_disconnect);
connect(client,SIGNAL(disconnected(Client*)),this,SLOT(remove_client(Client*)));
QThreadPool::globalInstance()->start(client);
}
With QThreadPool, there's no need to check whether the max number of threads has been reached or not. If the maximum has been reached, any new calls to start() will queue the next connection until a thread becomes available.
3.The reason your socket is not reading any data unless you call waitForReadyRead() is because you're executing the run() function in the main thread, creating the local socket in the main thread, you make a DirectConnection with the instance of Client and then move client to a different thread. You cannot have direct connections between threads.
You will need to add a local QEventLoop to your run() function to handle all events and signals of your new thread but remember to connect signals to your loop's quit() slot so the run() function will exit, otherwise your thread will continue to run forever.

QUdpSocket doesn't emit readyRead() signal

I faced a problem with QUdpSocket. Signal readyRead() seems to be never emitted. So, I decided to create QTimer and check state of socket reading queue. That way I ensured that socket working properly (bytesAvailable() shows number of bytes) and signal/slot mechanism is working too (timeout() signal occurred). But why readyRead() doesn't emit? Thanks.
Qt 5.1
QString EthernetListener::listen()
{
udp_socket = new QUdpSocket(this);
connect(udp_socket, SIGNAL(readyRead()), this, SLOT(process_messages()));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(dummy_slot()));
timer->start(1000);
bool res = udp_socket->bind(QHostAddress::Any, 1947, QUdpSocket::ShareAddress);
if (!res)
return QString("Не удалось подключиться к хосту").toUtf8();
return QString("Идет прослушка сети. Хост: ");
}
void EthernetListener::dummy_slot()
{
int test = udp_socket->bytesAvailable();
}
void EthernetListener::process_messages()
{
bool bp = true;
}
This problem can occur if more data becomes available while still processing the first datagram. Add this to the slot and it should work fine:
int readCount;
while (udpSock->hasPendingDatagrams())
{
readCount = udpSock->readDatagram(buffer, 4096);
cout << "readCount = " << readCount << endl;
}
You can try implementing basic receiver using this one: Udp Receiver
Probably you are not reading correctly from the slot so it looks like the signal is not emitted. Or you are calling bind after you connected the slot: probably you should call bind before connecting the slot.