From: Soeren Apel Date: Fri, 4 Sep 2015 01:34:08 +0000 (+0200) Subject: Fix #605 by closing current device when another is selected X-Git-Tag: pulseview-0.3.0~128 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=4d6c6ea3e6b069787c270d4911083dae05eae4c6 Fix #605 by closing current device when another is selected As the device manager class holds a pointer to all devices they will never be destroyed until PV exits. Devices are opened with the first call to create() but only closed in the destructor. Together, this results in devices never being closed. This patch fixes this by renaming create() to open() and introducing a matching close() method that the session class calls when a different device is to be selected. --- diff --git a/pv/devices/device.hpp b/pv/devices/device.hpp index 0a8f2438..acc772ec 100644 --- a/pv/devices/device.hpp +++ b/pv/devices/device.hpp @@ -64,7 +64,9 @@ public: virtual std::string display_name( const DeviceManager &device_manager) const = 0; - virtual void create() = 0; + virtual void open() = 0; + + virtual void close() = 0; virtual void start(); diff --git a/pv/devices/hardwaredevice.cpp b/pv/devices/hardwaredevice.cpp index b98b0e02..d1161aa2 100644 --- a/pv/devices/hardwaredevice.cpp +++ b/pv/devices/hardwaredevice.cpp @@ -49,11 +49,7 @@ HardwareDevice::HardwareDevice(const std::shared_ptr &context, } HardwareDevice::~HardwareDevice() { - if (device_open_) - device_->close(); - - if (session_) - session_->remove_devices(); + close(); } string HardwareDevice::full_name() const { @@ -99,8 +95,10 @@ string HardwareDevice::display_name( return join(parts, " "); } -void HardwareDevice::create() { - // Open the device +void HardwareDevice::open() { + if (device_open_) + close(); + try { device_->open(); } catch(const sigrok::Error &e) { @@ -114,5 +112,15 @@ void HardwareDevice::create() { session_->add_device(device_); } +void HardwareDevice::close() { + if (device_open_) + device_->close(); + + if (session_) + session_->remove_devices(); + + device_open_ = false; +} + } // namespace devices } // namespace pv diff --git a/pv/devices/hardwaredevice.hpp b/pv/devices/hardwaredevice.hpp index df11ed25..bc8e47a5 100644 --- a/pv/devices/hardwaredevice.hpp +++ b/pv/devices/hardwaredevice.hpp @@ -53,7 +53,9 @@ public: */ std::string display_name(const DeviceManager &device_manager) const; - void create(); + void open(); + + void close(); private: const std::shared_ptr context_; diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp index e92bf208..13af3db9 100644 --- a/pv/devices/inputfile.cpp +++ b/pv/devices/inputfile.cpp @@ -42,10 +42,18 @@ InputFile::InputFile(const std::shared_ptr &context, throw QString("Failed to create input"); } -void InputFile::create() { +void InputFile::open() { + if (session_) + close(); + session_ = context_->create_session(); } +void InputFile::close() { + if (session_) + session_->remove_devices(); +} + void InputFile::start() { } diff --git a/pv/devices/inputfile.hpp b/pv/devices/inputfile.hpp index 588ccdf2..9b9aee88 100644 --- a/pv/devices/inputfile.hpp +++ b/pv/devices/inputfile.hpp @@ -41,7 +41,9 @@ public: std::shared_ptr format, const std::map &options); - void create(); + void open(); + + void close(); void start(); diff --git a/pv/devices/sessionfile.cpp b/pv/devices/sessionfile.cpp index d45b9fa4..e6920ff5 100644 --- a/pv/devices/sessionfile.cpp +++ b/pv/devices/sessionfile.cpp @@ -33,10 +33,18 @@ SessionFile::SessionFile(const std::shared_ptr context, context_(context) { } -void SessionFile::create() { +void SessionFile::open() { + if (session_) + close(); + session_ = context_->load_session(file_name_); device_ = session_->devices()[0]; } +void SessionFile::close() { + if (session_) + session_->remove_devices(); +} + } // namespace devices } // namespace pv diff --git a/pv/devices/sessionfile.hpp b/pv/devices/sessionfile.hpp index 0a546f0c..94945714 100644 --- a/pv/devices/sessionfile.hpp +++ b/pv/devices/sessionfile.hpp @@ -38,7 +38,9 @@ public: SessionFile(const std::shared_ptr context, const std::string &file_name); - void create(); + void open(); + + void close(); private: const std::shared_ptr context_; diff --git a/pv/session.cpp b/pv/session.cpp index 22e0428d..48235432 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -127,8 +127,11 @@ void Session::set_device(shared_ptr device) // Ensure we are not capturing before setting the device stop_capture(); + if (device_) + device_->close(); + device_ = std::move(device); - device_->create(); + device_->open(); device_->session()->add_datafeed_callback([=] (shared_ptr device, shared_ptr packet) { data_feed_in(device, packet);