From 322ff31d6a7a8339c9a28353d7a0c97b2981c3ef Mon Sep 17 00:00:00 2001 From: ForSuhr Date: Sat, 16 Sep 2023 20:30:47 +0200 Subject: [PATCH] Network: fix bug of connection to nullptr --- desktop/FlowLink.cpp | 14 ++++++++++---- desktop/windows/ChatWindow.cpp | 5 +++++ desktop/windows/ChatWindow.h | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/desktop/FlowLink.cpp b/desktop/FlowLink.cpp index 75a9eb5..476e60a 100644 --- a/desktop/FlowLink.cpp +++ b/desktop/FlowLink.cpp @@ -282,9 +282,11 @@ void FlowLink::createChatWindow(NetworkManager *network) (*m_chatWindowMap)[device.address] = chatWindow; // shift pointers - chatWindow->m_tcpReceiver = network->m_tcpReceiver; - chatWindow->m_tcpSender = network->m_tcpSender; - chatWindow->setupMsgReceiveConnection(); + (*m_chatWindowMap)[device.address]->m_tcpReceiver = network->m_tcpReceiver; + (*m_chatWindowMap)[device.address]->m_tcpSender = network->m_tcpSender; + + // setup connection to receive msg + (*m_chatWindowMap)[device.address]->setupMsgReceiveConnection(); // create a connection to notify the progress window that there is a new download task connect((*m_chatWindowMap)[device.address]->m_tcpReceiver, &TcpReceiver::startNewTaskSignal, m_progressWindow, &ProgressWindow::createProgressWidget); @@ -318,6 +320,7 @@ void FlowLink::onDisconnectActionClicked() { pair.second->m_tcpReceiver->closeConnection(); pair.second->m_tcpSender->disconnectFromHost(); + pair.second->destroyMsgReceiveConnection(); pair.second->deleteLater(); // delete the chatwindow } @@ -408,6 +411,7 @@ void FlowLink::removeDevice(Device device) { pair.second->m_tcpReceiver->closeConnection(); pair.second->m_tcpSender->disconnectFromHost(); + pair.second->destroyMsgReceiveConnection(); pair.second->deleteLater(); } } @@ -420,9 +424,11 @@ void FlowLink::removeDevice(Device device) /// @brief remove all devices from the table view and delete all chat windows void FlowLink::removeDevices() { - // remove all devices from the table + // remove all devices from the table, except for local host device int rowNum = m_deviceTableModel->rowCount(QModelIndex()); m_deviceTableModel->removeRows(0, rowNum, QModelIndex()); + if (m_isShowLocalHost && m_localHostDevice != nullptr) + m_deviceTableModel->addRow(m_localHostDevice->name, m_localHostDevice->address); // clear the chat window map m_chatWindowMap->clear(); diff --git a/desktop/windows/ChatWindow.cpp b/desktop/windows/ChatWindow.cpp index a9d08f9..d4c1949 100644 --- a/desktop/windows/ChatWindow.cpp +++ b/desktop/windows/ChatWindow.cpp @@ -23,6 +23,11 @@ void ChatWindow::setupMsgReceiveConnection() { leftAlignedAppend(this, msg); }); } +void ChatWindow::destroyMsgReceiveConnection() +{ + disconnect(m_tcpReceiver, &TcpReceiver::msgSignal, nullptr, nullptr); +} + QString ChatWindow::msgText() { return ui->lineEditMsg->text(); diff --git a/desktop/windows/ChatWindow.h b/desktop/windows/ChatWindow.h index 8b0abd7..6b3eae4 100644 --- a/desktop/windows/ChatWindow.h +++ b/desktop/windows/ChatWindow.h @@ -26,6 +26,7 @@ class ChatWindow : public QWidget void setUpTcpReceiver(int port); void setUpTcpSender(QString address, int port); void setupMsgReceiveConnection(); + void destroyMsgReceiveConnection(); QString msgText(); /* friend functions */