I thought I was going crazy, but my heap-allocated values are being modified while being passed to cout <<
.
To generally describe my setup, I've got a ring buffer containing classes which are basically (value
, timestamp
) tuples with (double
, double
) type. I'm attempting to implement a signal delay such that values passed into this buffer are held in the buffer for some fixed delay before finally being emitted. I've initialized a buffer size 1024 of these objects with some known initial condition and then upon receiving a new input value I peek at the head of the list to see if the difference between it's timestamp and the new value's timestamp is greater than the delay. If it is, I pop the head of the list off and discard it. Finally I return the current head of the list.
This all works pretty much as I'd expect it.
If I add a simple print statement (in my main, after invoking the delay filter):
std::cout << "Signal out of delay filter: (" << s.get << ", " << s.get_timestamp() << ").\n";
However, everything explodes. Suddenly elements in my ring buffer are filled with garbage data which breaks my timestamp comparisons.
Any idea? Here's some code snippets that might be relevant, I can post more on request.
edit: I've set a watchpoint on the first entry in my buffer to see where it's modified and it's definitely inside the cout <<
call. Here's what GDB had to say
Hardware watchpoint 2: d.buf.buffer[0]
Old value = {value = 0, timestamp = 0}
New value = {value = 4.10047448604823463e-322, timestamp = 0}
_IO_new_file_overflow (f=0x7ffff783a620 <_IO_2_1_stdout_>, ch=83) at fileops.c:857 857:
fileops.c: No such file or directory
edit 2: I'm not sure how to properly attach an MVCE, but here's the contents of the files. I hope this is minimal enough, but it captures the problem so it's at least a CVE. If this is too much I can try to cut it down further but I run the risk of eliminating the bug if it gets much more minimal:
CMakeLists.txt:
# Project Definiton
cmake_minimum_required(VERSION 3.5)
project("ring_buffer_mvce")
set_property(GLOBAL PROPERTY CXX_STANDARD 14)
set_property(GLOBAL PROPERTY CXX_STANDARD_REQUIRED ON)
# Automated Unit Test Configuration
enable_testing()
add_executable(delay_test
src/signals/delay.cpp
src/test/delay_test.cpp)
add_test(delay_test delay_test)
src/signals/delay.cpp
#include "delay.hpp"
delay::delay(double time, size_t buffer_size, signal<double> initial_condition) {
this->time = time;
buf = ring_buffer<signal<double> >(buffer_size);
for (int i = 0; i < buffer_size; i++) {
buf.push(initial_condition);
}
}
delay::~delay() {
}
signal<double> delay::apply(signal<double> s) {
// Add the current signal to the delay buffer
buf.push(s);
// Check to see if our current time has elapsed
double cur_time_delta = s.get_timestamp() - buf.peek().get_timestamp();
if (cur_time_delta >= (time - DELAY_EPSILON)) {
buf.pop();
}
return buf.peek();
}
src/signals/delay.hpp:
#ifndef __DELAY_H__
#define __DELAY_H__
#include <cstddef>
#include <limits>
#include "signal.hpp"
#include "filter.hpp"
#include "../utils.h"
#define DELAY_EPSILON 0.00001
class delay : public filter<double> {
public:
delay(double time, size_t buffer_size, signal<double> initial_condition);
~delay();
virtual signal<double> apply(const signal<double>);
private:
double time;
ring_buffer<signal<double> > buf;
};
#endif
src/signals/filter.hpp:
#ifndef __FILTER_HPP__
#define __FILTER_HPP__
#include "signal.hpp"
template <class t>
class filter {
public:
virtual signal<t> apply(const signal<t>) = 0;
};
#endif
src/signals/signal.hpp:
#ifndef __SIGNAL_HPP__
#define __SIGNAL_HPP__
#include <algorithm>
template <class t>
class signal {
public:
signal<t>(t value, double timestamp);
signal<t>();
t get();
double get_timestamp();
private:
t value;
double timestamp;
};
template <class t>
signal<t>::signal(t value, double timestamp) {
this->value = value;
this->timestamp = timestamp;
}
template <class t>
signal<t>::signal() {
timestamp = -1;
}
template <class t>
double signal<t>::get_timestamp() {
return timestamp;
}
template <class t>
t signal<t>::get() {
return value;
}
#endif
src/utils.h:
#ifndef __UTILS_H__
#define __UTILS_H__
#include <cstring>
template <class t>
class ring_buffer {
public:
ring_buffer(size_t buffer_size);
ring_buffer();
~ring_buffer();
void push(t data);
t peek();
t pop();
private:
t* buffer;
int data_start_idx;
int data_end_idx;
size_t size;
};
template <class t>
ring_buffer<t>::ring_buffer(size_t buffer_size) {
buffer = new t[buffer_size];
size = buffer_size;
data_start_idx = 0;
data_end_idx = 0;
}
template <class t>
ring_buffer<t>::ring_buffer() {
ring_buffer(64);
}
template <class t>
ring_buffer<t>::~ring_buffer() {
delete[] buffer;
}
template <class t>
t ring_buffer<t>::peek() {
return buffer[data_start_idx];
}
template <class t>
t ring_buffer<t>::pop() {
t data = buffer[data_start_idx];
data_start_idx = (data_start_idx + 1) % size;
return data;
}
template <class t>
void ring_buffer<t>::push(t data) {
buffer[data_end_idx] = data;
data_end_idx = (data_end_idx + 1) % size;
}
#endif
src/test/delay_test.cpp:
#include <iostream>
#include <stdio.h>
#include "../signals/signal.hpp"
#include "../signals/filter.hpp"
#include "../signals/delay.hpp"
int main(int argc, char **argv) {
delay d = delay(0.5, 1024, signal<double>(0, 0));
double sig = 0;
double t = 0;
signal<double> s = signal<double>(0, 0);
while (true) {
s = d.apply(signal<double>(sig, t));
std::cout << "Signal out of delay filter: (" << s.get() << ", " << s.get_timestamp() << ").\n";
sig += 1;
t += 0.1;
getchar();
}
return 0;
}