Skip to content

Commit

Permalink
Network: fix bug of connection to nullptr
Browse files Browse the repository at this point in the history
  • Loading branch information
ForSuhr committed Sep 16, 2023
1 parent 2ef4e70 commit 322ff31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions desktop/FlowLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions desktop/windows/ChatWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions desktop/windows/ChatWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 322ff31

Please sign in to comment.