Can't pass a signal with QSharedPointer (or std::shared_ptr<>) between threads
-
wrote on 27 Feb 2017, 14:05 last edited by michalos
Hi
I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
When I try to catch an empty signal, everything works fine.My code :
//creating the QShared ptr // std::shared_ptr<Message> msgShared (new Message()); QSharedPointer<Message> message(new Message()); // emiting : (works) emit parsingTextMessageFinished(message);
//different class: void PBXReader::initHandlers(){ smsHandler = new SMSTagHandler(req_ans_mangr, this); connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler); } void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg) { emit parsingTextMessageFinShared(msg); emit parsingTextMessageFin(); }
and in the main thread:
public slots: void gotParsingTextMessage(QSharedPointer<Message>); void gotParsingTextMesg(); connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg); connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
Does anyone got any idea why?
-
Hi
I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
When I try to catch an empty signal, everything works fine.My code :
//creating the QShared ptr // std::shared_ptr<Message> msgShared (new Message()); QSharedPointer<Message> message(new Message()); // emiting : (works) emit parsingTextMessageFinished(message);
//different class: void PBXReader::initHandlers(){ smsHandler = new SMSTagHandler(req_ans_mangr, this); connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler); } void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg) { emit parsingTextMessageFinShared(msg); emit parsingTextMessageFin(); }
and in the main thread:
public slots: void gotParsingTextMessage(QSharedPointer<Message>); void gotParsingTextMesg(); connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg); connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
Does anyone got any idea why?
Hi normally
Signals between threads is Qt::QueuedConnection
and you must use type that is known how to queue.I wonder if QSharedPointer<Message>
is seens as new type.You dont get any warnings ?
-
wrote on 27 Feb 2017, 14:56 last edited by
@mrjj
Thank You for Your answer.
I was wondering the same thing.
I don't get any info, that I should register a metatype (qRegisterMetaType).
I do not get any warnings nor debug info.
When I use a C style pointer (as below) everything works fine.Message *message = new Message(this);
-
@mrjj
Thank You for Your answer.
I was wondering the same thing.
I don't get any info, that I should register a metatype (qRegisterMetaType).
I do not get any warnings nor debug info.
When I use a C style pointer (as below) everything works fine.Message *message = new Message(this);
@michalos
Yes it was the qRegisterMetaType i was thinking of.
But it seems you are fully aware of this and would see any warning so its not htat.Next question is.
Does it go out of scope? ( for some reason)
-
wrote on 27 Feb 2017, 15:13 last edited by michalos
@mrjj
That is a good question.
I do not know if it's out of scope.If I create it like that:
QSharedPointer<Message> message(new Message());
or
std::shared_ptr<Message> msgShared (new Message());
will it go out of scope?
Isn't it created on the heap? -
@mrjj
That is a good question.
I do not know if it's out of scope.If I create it like that:
QSharedPointer<Message> message(new Message());
or
std::shared_ptr<Message> msgShared (new Message());
will it go out of scope?
Isn't it created on the heap?I'm not sure. ( its with new so ..)
Is Message your own class?
I wonder if you can put aDebug() or breakpoint in the destructor and see if it dies when copied..
Also make sure you have Qt::QueuedConnection on your connects
-
wrote on 28 Feb 2017, 08:17 last edited by
I've changed the connection to Qt::QueuedConnection and it worked :)
I thought that this is done automatically when connecting between threads, but in this case it didn't.
Thank You for Your help :)
-
I've changed the connection to Qt::QueuedConnection and it worked :)
I thought that this is done automatically when connecting between threads, but in this case it didn't.
Thank You for Your help :)
@michalos
Oh. super.
I do wonder how it could work with no QueuedConnection and
no QSharedPointer but I guess its just random :) -
wrote on 28 Feb 2017, 14:18 last edited by
I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.
-
I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.
Well, seems its all explained here
http://doc.qt.io/qt-5/threads-qobject.html
I guess that Auto Connection does guess. :)
3/10