Skip to content

Commit

Permalink
Fix #605 by closing current device when another is selected
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abraxa committed Sep 4, 2015
1 parent accd9c0 commit 4d6c6ea
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
4 changes: 3 additions & 1 deletion pv/devices/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class Device
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();

Expand Down
22 changes: 15 additions & 7 deletions pv/devices/hardwaredevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ HardwareDevice::HardwareDevice(const std::shared_ptr<sigrok::Context> &context,
}

HardwareDevice::~HardwareDevice() {
if (device_open_)
device_->close();

if (session_)
session_->remove_devices();
close();
}

string HardwareDevice::full_name() const {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
4 changes: 3 additions & 1 deletion pv/devices/hardwaredevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class HardwareDevice final : public Device
*/
std::string display_name(const DeviceManager &device_manager) const;

void create();
void open();

void close();

private:
const std::shared_ptr<sigrok::Context> context_;
Expand Down
10 changes: 9 additions & 1 deletion pv/devices/inputfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ InputFile::InputFile(const std::shared_ptr<sigrok::Context> &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() {
}

Expand Down
4 changes: 3 additions & 1 deletion pv/devices/inputfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class InputFile final : public File
std::shared_ptr<sigrok::InputFormat> format,
const std::map<std::string, Glib::VariantBase> &options);

void create();
void open();

void close();

void start();

Expand Down
10 changes: 9 additions & 1 deletion pv/devices/sessionfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ SessionFile::SessionFile(const std::shared_ptr<sigrok::Context> 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
4 changes: 3 additions & 1 deletion pv/devices/sessionfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class SessionFile final : public File
SessionFile(const std::shared_ptr<sigrok::Context> context,
const std::string &file_name);

void create();
void open();

void close();

private:
const std::shared_ptr<sigrok::Context> context_;
Expand Down
5 changes: 4 additions & 1 deletion pv/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ void Session::set_device(shared_ptr<devices::Device> 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<sigrok::Device> device, shared_ptr<Packet> packet) {
data_feed_in(device, packet);
Expand Down

0 comments on commit 4d6c6ea

Please sign in to comment.