From e333d015458b579768ca229918a3ed426f0e4029 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Fri, 8 Nov 2019 22:31:14 +0100 Subject: CAN Example: Don't destroy QCanBusDevice after disconnect The old code could already called the destructor of the CAN plugin while the disconnect process was still running. Using deleteLater() does not help much here, e.g. the VirtualCAN plugin needs to send "disconnect" packets and then close the TCP connection afterwards. By using a unique_ptr we can keep the object alive until the program ends or the connection dialog is opened to set up a new connection. By this delay and the usage of deleteLater(), the plugin has enough time to properly shut down. Change-Id: I9e992a4b954acd82d62d058000d2108d975b1e9c Reviewed-by: Alex Blasche --- examples/serialbus/can/mainwindow.cpp | 25 +++++++++++++------------ examples/serialbus/can/mainwindow.h | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'examples/serialbus') diff --git a/examples/serialbus/can/mainwindow.cpp b/examples/serialbus/can/mainwindow.cpp index 29c5f1f..a1c10fe 100644 --- a/examples/serialbus/can/mainwindow.cpp +++ b/examples/serialbus/can/mainwindow.cpp @@ -79,8 +79,6 @@ MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() { - delete m_canDevice; - delete m_connectDialog; delete m_ui; } @@ -91,7 +89,10 @@ void MainWindow::initActionsConnections() m_ui->sendFrameBox->setEnabled(false); connect(m_ui->sendFrameBox, &SendFrameBox::sendFrame, this, &MainWindow::sendFrame); - connect(m_ui->actionConnect, &QAction::triggered, m_connectDialog, &ConnectDialog::show); + connect(m_ui->actionConnect, &QAction::triggered, [this]() { + m_canDevice.release()->deleteLater(); + m_connectDialog->show(); + }); connect(m_connectDialog, &QDialog::accepted, this, &MainWindow::connectDevice); connect(m_ui->actionDisconnect, &QAction::triggered, this, &MainWindow::disconnectDevice); connect(m_ui->actionResetController, &QAction::triggered, this, [this]() { @@ -125,8 +126,8 @@ void MainWindow::connectDevice() const ConnectDialog::Settings p = m_connectDialog->settings(); QString errorString; - m_canDevice = QCanBus::instance()->createDevice(p.pluginName, p.deviceInterfaceName, - &errorString); + m_canDevice.reset(QCanBus::instance()->createDevice(p.pluginName, p.deviceInterfaceName, + &errorString)); if (!m_canDevice) { m_status->setText(tr("Error creating device '%1', reason: '%2'") .arg(p.pluginName).arg(errorString)); @@ -135,9 +136,12 @@ void MainWindow::connectDevice() m_numberFramesWritten = 0; - connect(m_canDevice, &QCanBusDevice::errorOccurred, this, &MainWindow::processErrors); - connect(m_canDevice, &QCanBusDevice::framesReceived, this, &MainWindow::processReceivedFrames); - connect(m_canDevice, &QCanBusDevice::framesWritten, this, &MainWindow::processFramesWritten); + connect(m_canDevice.get(), &QCanBusDevice::errorOccurred, + this, &MainWindow::processErrors); + connect(m_canDevice.get(), &QCanBusDevice::framesReceived, + this, &MainWindow::processReceivedFrames); + connect(m_canDevice.get(), &QCanBusDevice::framesWritten, + this, &MainWindow::processFramesWritten); if (p.useConfigurationEnabled) { for (const ConnectDialog::ConfigurationItem &item : p.configurations) @@ -147,8 +151,7 @@ void MainWindow::connectDevice() if (!m_canDevice->connectDevice()) { m_status->setText(tr("Connection error: %1").arg(m_canDevice->errorString())); - delete m_canDevice; - m_canDevice = nullptr; + m_canDevice.reset(); } else { m_ui->actionConnect->setEnabled(false); m_ui->actionDisconnect->setEnabled(true); @@ -210,8 +213,6 @@ void MainWindow::disconnectDevice() m_busStatusTimer->stop(); m_canDevice->disconnectDevice(); - delete m_canDevice; - m_canDevice = nullptr; m_ui->actionConnect->setEnabled(true); m_ui->actionDisconnect->setEnabled(false); diff --git a/examples/serialbus/can/mainwindow.h b/examples/serialbus/can/mainwindow.h index 673e87a..2e910d1 100644 --- a/examples/serialbus/can/mainwindow.h +++ b/examples/serialbus/can/mainwindow.h @@ -96,7 +96,7 @@ private: QLabel *m_status = nullptr; QLabel *m_written = nullptr; ConnectDialog *m_connectDialog = nullptr; - QCanBusDevice *m_canDevice = nullptr; + std::unique_ptr m_canDevice; QTimer *m_busStatusTimer = nullptr; }; -- cgit v1.2.3