From: Joel Holdsworth Date: Fri, 15 Mar 2013 18:28:28 +0000 (+0000) Subject: Analog buffer sharing crash by memcpying for the AnanlogSignal::paint X-Git-Tag: pulseview-0.1.0~82 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d37583678256450d7eb646213d0b9e170a427933;p=pulseview.git Analog buffer sharing crash by memcpying for the AnanlogSignal::paint --- diff --git a/pv/data/analogsnapshot.cpp b/pv/data/analogsnapshot.cpp index 2bcb99f3..1658a04c 100644 --- a/pv/data/analogsnapshot.cpp +++ b/pv/data/analogsnapshot.cpp @@ -49,9 +49,21 @@ void AnalogSnapshot::append_payload( append_data(analog.data, analog.num_samples); } -const float* AnalogSnapshot::get_samples() const +const float* AnalogSnapshot::get_samples( + int64_t start_sample, int64_t end_sample) const { - return (const float*)_data; + assert(start_sample >= 0); + assert(start_sample < (int64_t)_sample_count); + assert(end_sample >= 0); + assert(end_sample < (int64_t)_sample_count); + assert(start_sample <= end_sample); + + lock_guard lock(_mutex); + + float *const data = new float[end_sample - start_sample]; + memcpy(data, (float*)_data + start_sample, sizeof(float) * + (end_sample - start_sample)); + return data; } } // namespace data diff --git a/pv/data/analogsnapshot.h b/pv/data/analogsnapshot.h index 9ca08f1e..a4539184 100644 --- a/pv/data/analogsnapshot.h +++ b/pv/data/analogsnapshot.h @@ -36,7 +36,8 @@ public: void append_payload(const sr_datafeed_analog &analog); - const float* get_samples() const; + const float* get_samples(int64_t start_sample, + int64_t end_sample) const; }; } // namespace data diff --git a/pv/view/analogsignal.cpp b/pv/view/analogsignal.cpp index 209512b5..a6f032b5 100644 --- a/pv/view/analogsignal.cpp +++ b/pv/view/analogsignal.cpp @@ -68,18 +68,21 @@ void AnalogSignal::paint(QPainter &p, int y, int left, int right, double scale, (int64_t)0), last_sample); const int64_t end_sample = min(max((int64_t)ceil(end), (int64_t)0), last_sample); + const int64_t sample_count = end_sample - start_sample; - const float* samples = snapshot->get_samples(); + const float* samples = snapshot->get_samples( + start_sample, end_sample); assert(samples); - QPointF *points = new QPointF[end_sample - start_sample]; + QPointF *points = new QPointF[sample_count]; QPointF *point = points; for (int64_t sample = start_sample; sample != end_sample; sample++) { const float x = (sample / samples_per_pixel - pixels_offset) + left; - *point++ = QPointF(x, samples[sample] + y); + *point++ = QPointF(x, + samples[sample - start_sample] + y); } p.setPen(_colour);