fixed bug caused by accessing Settings from non Qt thread

This commit is contained in:
csoler 2019-12-07 22:26:26 +01:00
parent f118b4656e
commit b6ca07aaa3
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
4 changed files with 14 additions and 9 deletions

View file

@ -243,6 +243,8 @@ struct RsConnectionEvent : RsEvent
RsEvent::serial_process(j, ctx); RsEvent::serial_process(j, ctx);
RS_SERIAL_PROCESS(mConnectionInfoCode); RS_SERIAL_PROCESS(mConnectionInfoCode);
RS_SERIAL_PROCESS(mSslId); RS_SERIAL_PROCESS(mSslId);
RS_SERIAL_PROCESS(mStrInfo1);
RS_SERIAL_PROCESS(mStrInfo2);
} }
}; };

View file

@ -185,20 +185,21 @@ void NewsFeed::sortChanged(int index)
// handler for the new notification system in libretroshare. // handler for the new notification system in libretroshare.
void NewsFeed::handleEvent(std::shared_ptr<const RsEvent> event) void NewsFeed::handleEvent(std::shared_ptr<const RsEvent> event)
{
// /!\ Absolutely no access to Qt structures (such as Settings) should happen here!!!
RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this );
}
void NewsFeed::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{ {
uint flags = Settings->getNewsFeedFlags(); uint flags = Settings->getNewsFeedFlags();
if(event->mType == RsEventType::AUTHSSL_CONNECTION_AUTENTICATION && (flags & RS_FEED_TYPE_SECURITY)) if(event->mType == RsEventType::AUTHSSL_CONNECTION_AUTENTICATION && (flags & RS_FEED_TYPE_SECURITY))
{ handleSecurityEvent(event);
RsQThreadUtils::postToObject( [=]() { handleSecurityEvent(event); }, this );
return;
}
if(event->mType == RsEventType::PEER_CONNECTION && (flags & RS_FEED_TYPE_PEER)) if(event->mType == RsEventType::PEER_CONNECTION && (flags & RS_FEED_TYPE_PEER))
{ handleConnectionEvent(event);
RsQThreadUtils::postToObject( [=]() { handleConnectionEvent(event); }, this );
return;
}
} }
void NewsFeed::handleConnectionEvent(std::shared_ptr<const RsEvent> event) void NewsFeed::handleConnectionEvent(std::shared_ptr<const RsEvent> event)

View file

@ -104,6 +104,7 @@ private slots:
private: private:
void handleSecurityEvent(std::shared_ptr<const RsEvent> event); void handleSecurityEvent(std::shared_ptr<const RsEvent> event);
void handleConnectionEvent(std::shared_ptr<const RsEvent> event); void handleConnectionEvent(std::shared_ptr<const RsEvent> event);
void handleEvent_main_thread(std::shared_ptr<const RsEvent> event);
void addFeedItem(FeedItem *item); void addFeedItem(FeedItem *item);
void addFeedItemIfUnique(FeedItem *item, bool replace); void addFeedItemIfUnique(FeedItem *item, bool replace);

View file

@ -177,6 +177,7 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent)
ui->filterLineEdit->setPlaceholderText(tr("Search")) ; ui->filterLineEdit->setPlaceholderText(tr("Search")) ;
ui->filterLineEdit->showFilterIcon(); ui->filterLineEdit->showFilterIcon();
mEventHandlerId=0; // forces initialization
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> e) { handleEvent(e); }, mEventHandlerId ); rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> e) { handleEvent(e); }, mEventHandlerId );
mModel = new RsFriendListModel(); mModel = new RsFriendListModel();
@ -257,7 +258,7 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent)
void NewFriendList::handleEvent(std::shared_ptr<const RsEvent> e) void NewFriendList::handleEvent(std::shared_ptr<const RsEvent> e)
{ {
if(dynamic_cast<const RsConnectionEvent*>(e.get()) != nullptr) if(e->mType == RsEventType::PEER_CONNECTION)
{ {
// /!\ The function we're in is called from a different thread. It's very important // /!\ The function we're in is called from a different thread. It's very important
// to use this trick in order to avoid data races. // to use this trick in order to avoid data races.