title: Seg. Fault on exit caused by pointer to vector of pointers to struct
body: <p>After a few days of delaying this question and debugging I've found that If my code below is run after removing the <em>vector</em> from the union (and all of the code mentioning this vector) the seg. fault dissapears.</p>
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  
  struct Task
  {
    std::string Name;
  
    std::vector&lt;Task*&gt; *Subtasks;
    std::function&lt;void(std::string const &amp;)&gt; Job;
  
    Task() {}
    Task(std::string const &amp;arg_0) { Name = arg_0; }
    Task(std::string const &amp;arg_0, std::vector&lt;Task*&gt; *arg_1) { Name = arg_0; Subtasks = arg_1; }
    Task(std::string const &amp;arg_0, std::function&lt;void(std::string const &amp;)&gt; arg_1)
    { Name = arg_0; Job = arg_1; }
  
    ~Task() { for (auto tItem : *Subtasks) { delete tItem; } }
  };
  
  class Console
  {
  private:
    std::vector&lt;Task*&gt; 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(&quot;Name&quot;);
      //Task *tTask = new Task(&quot;Name&quot;, [this](std::string const &amp;arg_0){ ; });
     //Seg. fault still remains.
      add(tTask);
    }
  };
  
  int main()
  {
      Console _Console;
      _Console.foo();
  }
  </code></pre>
  <p><a href="http://ideone.com/i0p8jd" rel="nofollow noreferrer">For quick online IDE code test</a></p>
  <hr />
  <p>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?</p>
  <h1>EDIT1</h1>
  <p>Removed the union of vector and std::function</p>
  <h1>EDIT2</h1>
  <p>The Seg. Fault is most likely caused by the deconstructor <em>~Task();</em>.</p>