After a few days of delaying this question and debugging I've found that If my code below is run after removing the vector from the union (and all of the code mentioning this vector) the seg. fault dissapears.
#include <string>
#include <vector>
#include <functional>
struct Task
{
std::string Name;
std::vector<Task*> *Subtasks;
std::function<void(std::string const &)> Job;
Task() {}
Task(std::string const &arg_0) { Name = arg_0; }
Task(std::string const &arg_0, std::vector<Task*> *arg_1) { Name = arg_0; Subtasks = arg_1; }
Task(std::string const &arg_0, std::function<void(std::string const &)> arg_1)
{ Name = arg_0; Job = arg_1; }
~Task() { for (auto tItem : *Subtasks) { delete tItem; } }
};
class Console
{
private:
std::vector<Task*> Routine;
public:
~Console() { for (auto tItem : Routine) { delete tItem; } } //I thought that this is not needed but Valgrind thinks otherwise, strangely the deconstructors of the Tasks are not called, I guess.
void add(Task *arg_0) { Routine.push_back(arg_0); }
void foo()
{
Task *tTask = new Task();
//Task *tTask = new Task("Name");
//Task *tTask = new Task("Name", [this](std::string const &arg_0){ ; });
//Seg. fault still remains.
add(tTask);
}
};
int main()
{
Console _Console;
_Console.foo();
}
For quick online IDE code test
This should probably be another question, but I think it's too simple. I've heard before, that if a non-trivial is used in a union, it should be taken care of if the values are switched, how would one do that, and is it necessary if I do not intend to have any value changes on runtime?
EDIT1
Removed the union of vector and std::function
EDIT2
The Seg. Fault is most likely caused by the deconstructor ~Task();.