HELP! unordered_map inside class triggers segmentation fault #851
-
I think the problem is related to the initialization of the Arrangement type, but I'm at my wits end trying to fix this. The interesting part is that the Segmentation Fault only happens in the call from schedule of the timeline belonging to the track. The error is: Execution (inside a Catch2 test) Track<1u, 6u, 6u, Event> track;
Timeline<4, 2, Event> timeline;
auto first = TimelineEvents<4, Event>{{0, n8, 1}, {0, n4, 1}, {n2, n4, 2}, {n4, n8, 2}};
timeline.schedule(first);
auto &pattern = track.pattern();
auto second = TimelineEvents<4, Event>{Event{0, n8, 1}, Event{0, n4, 1}, Event{n2, n4, 2}, Event{n4, n8, 2}};
// >>>>SEGMENTATION FAULT TRIGGERED HERE<<<<<
pattern.timeline.schedule(second); track.h #pragma once
#include <playa/realtime/transport.h>
#include <playa/realtime/timeline.h>
#include <playa/core/event.h>
#include <playa/core/time.h>
#include <cstdint>
#include <cstddef>
namespace playa
{
template <std::size_t UniqueEventsCount, std::size_t TimelineEventsCount, typename E = Event>
struct Pattern
{
Timeline<UniqueEventsCount, TimelineEventsCount, E> timeline;
TransportRegion playbackRegion;
Pattern() = default;
~Pattern() = default;
};
template <std::size_t PatternsCount, std::size_t UniqueEventsCount, std::size_t TimelineEventsCount, typename E = Event>
using Patterns = etl::vector<Pattern<UniqueEventsCount, TimelineEventsCount, E>, PatternsCount>;
template <std::size_t PatternsCount, std::size_t UniqueEventsCount, std::size_t TimelineEventsCount, typename E = Event>
class Track
{
private:
Patterns<PatternsCount, UniqueEventsCount, TimelineEventsCount, E> patterns_;
Transport transport_;
Transport *globalTransport_ = nullptr;
public:
Track() = default;
~Track()
{
if(globalTransport_ != nullptr)
{
globalTransport_->remove_observer(transport_);
}
};
// todo: create copy constructor and assignment operator are deleted
Track(const Track &other) = delete;
Track &operator=(const Track &other) = delete;
// todo: create move constructor and assignment operator
Track(Track &&other) = delete;
Track &operator=(Track &&other) = delete;
};
} // namespace playa timeline.h #pragma once
#include <cstdint>
#include <playa/core/event.h>
#include <etl/unordered_map.h>
#include <etl/deque.h>
#include <etl/algorithm.h>
namespace playa
{
template <std::size_t TimelineEventsCount, typename E = Event>
using TimelineEvents = etl::vector<E, TimelineEventsCount>;
template <std::size_t UniqueEventsCount, std::size_t TimelineEventsCount, typename E = Event>
using Arrangement
= etl::unordered_map<unsigned int, TimelineEvents<TimelineEventsCount, E>, UniqueEventsCount>;
template <std::size_t UniqueEventsCount, std::size_t TimelineEventsCount, typename E = Event>
class Timeline
{
private:
Arrangement<UniqueEventsCount, TimelineEventsCount, E> arrangement_;
etl::deque<unsigned int, UniqueEventsCount> timeline_;
public:
Timeline() : arrangement_(), timeline_() {};
~Timeline() = default;
//copy
Timeline(const Timeline&) = delete;
Timeline& operator=(const Timeline&) = delete;
// move
Timeline(Timeline&&) = delete;
Timeline& operator=(Timeline&&) = delete;
void schedule(E event)
{
unsigned int time = event.time;
if(arrangement_.empty()) // <<<<<<<< SEGMENTATION FAULT
{
arrangement_.insert({time, {event}});
timeline_.push_back(time);
return;
}
else if(arrangement_.find(time) == arrangement_.end())
{
arrangement_.insert({time, {event}});
auto it = etl::lower_bound(timeline_.begin(), timeline_.end(), time);
timeline_.insert(it, time);
}
else
{
if(arrangement_[time].size() < TimelineEventsCount)
{
arrangement_[time].push_back(event);
}
}
}
template <std::size_t M>
void schedule(const TimelineEvents<M, E>& events)
{
for(auto& event : events)
{
schedule(event);
}
}
const Arrangement<UniqueEventsCount, TimelineEventsCount, E>& arrangement() const { return arrangement_; }
const etl::deque<unsigned int, UniqueEventsCount>& timeline() const { return timeline_; }
};
|
Beta Was this translation helpful? Give feedback.
Answered by
ricardomatias
Feb 26, 2024
Replies: 1 comment
-
When you think of an array, use an array not a vector. That was the problem here. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ricardomatias
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you think of an array, use an array not a vector. That was the problem here.