diff options
author | Andre Hartmann <aha_1980@gmx.de> | 2019-11-08 22:31:14 +0100 |
---|---|---|
committer | Andre Hartmann <aha_1980@gmx.de> | 2019-11-21 21:01:56 +0100 |
commit | e333d015458b579768ca229918a3ed426f0e4029 (patch) | |
tree | 921e63de39ae759a9808d4919dec396f3c6d3572 /examples/serialbus | |
parent | 545494c6e7c2441f7fc8c6b7e337189461938b00 (diff) |
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 <alexander.blasche@qt.io>
Diffstat (limited to 'examples/serialbus')
-rw-r--r-- | examples/serialbus/can/mainwindow.cpp | 25 | ||||
-rw-r--r-- | examples/serialbus/can/mainwindow.h | 2 |
2 files changed, 14 insertions, 13 deletions
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<QCanBusDevice> m_canDevice; QTimer *m_busStatusTimer = nullptr; }; |