From e7216ae0a66fe1563514cbd3f67f2e240d010315 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Wed, 19 Aug 2015 21:34:11 +0200 Subject: [PATCH] Fix #626 by stopping acquisition gracefully --- pv/data/analogsegment.cpp | 1 + pv/data/segment.cpp | 3 ++- pv/session.cpp | 19 +++++++++++++++++-- pv/session.hpp | 2 ++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pv/data/analogsegment.cpp b/pv/data/analogsegment.cpp index c1a119e4..3703f0d0 100644 --- a/pv/data/analogsegment.cpp +++ b/pv/data/analogsegment.cpp @@ -69,6 +69,7 @@ void AnalogSegment::append_interleaved_samples(const float *data, lock_guard lock(mutex_); + // If we're out of memory, this will throw std::bad_alloc data_.resize((sample_count_ + sample_count) * sizeof(float)); float *dst = (float*)data_.data() + sample_count_; diff --git a/pv/data/segment.cpp b/pv/data/segment.cpp index 111b62b7..bc08fd07 100644 --- a/pv/data/segment.cpp +++ b/pv/data/segment.cpp @@ -78,8 +78,9 @@ void Segment::set_capacity(const uint64_t new_capacity) assert(capacity_ >= sample_count_); if (new_capacity > capacity_) { - capacity_ = new_capacity; + // If we're out of memory, this will throw std::bad_alloc data_.resize((new_capacity * unit_size_) + sizeof(uint64_t)); + capacity_ = new_capacity; } } diff --git a/pv/session.cpp b/pv/session.cpp index 56ac8997..22e0428d 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -419,6 +419,8 @@ void Session::sample_thread_proc(shared_ptr device, cur_samplerate_ = device_->read_config(ConfigKey::SAMPLERATE); + out_of_memory_ = false; + try { device_->start(); } catch(Error e) { @@ -438,6 +440,9 @@ void Session::sample_thread_proc(shared_ptr device, qDebug("SR_DF_END was not received."); assert(0); } + + if (out_of_memory_) + error_handler(tr("Out of memory, acquisition stopped.")); } void Session::feed_in_header() @@ -590,11 +595,21 @@ void Session::data_feed_in(shared_ptr device, break; case SR_DF_LOGIC: - feed_in_logic(dynamic_pointer_cast(packet->payload())); + try { + feed_in_logic(dynamic_pointer_cast(packet->payload())); + } catch (std::bad_alloc) { + out_of_memory_ = true; + device_->stop(); + } break; case SR_DF_ANALOG: - feed_in_analog(dynamic_pointer_cast(packet->payload())); + try { + feed_in_analog(dynamic_pointer_cast(packet->payload())); + } catch (std::bad_alloc) { + out_of_memory_ = true; + device_->stop(); + } break; case SR_DF_END: diff --git a/pv/session.hpp b/pv/session.hpp index 59060a59..3d303da4 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -174,6 +174,8 @@ private: std::thread sampling_thread_; + bool out_of_memory_; + Q_SIGNALS: void capture_state_changed(int state); void device_selected(); -- 2.30.2