title: fix memory leaks with valgrind
body: <p>I am working with c++ and been using valgrind to fix memory leaks.
  I am trying to optimize the following piece of code that valgrind characterizes as a leak:</p>
  
  <pre><code>void VOMC::sub_train(vector&lt;Letter&gt; tempLettersA, vector&lt;Letter&gt; tempLettersB) {
      int stateA_id = state_exists(tempLettersA);
      State *tempStateA;
  
      if (stateA_id != -1) {
          tempStateA = get_state_by_id(stateA_id);
      } else {
          tempStateA = new State(tempLettersA);// MEMORY LEAK - 1
          vomc.push_back(tempStateA);
      }
      int stateB_id = state_exists(tempLettersB);
      if (stateB_id != -1) {
          tempStateA-&gt;inc_state(stateB_id);
      } else {
          State* tempStateB;
          tempStateB = new State(tempLettersB);//MEMORY LEAK -2
          vomc.push_back(tempStateB);
          stateB_id = tempStateB-&gt;GetId();
          tempStateA-&gt;inc_state(stateB_id);
      }
  }
  </code></pre>
  
  <p>For memory leak-1 I get the following message </p>
  
  <pre><code>==11289== 4,055 (64 direct, 3,991 indirect) bytes in 1 blocks are definitely lost in   loss record 228 of 228
  ==11289==    at 0x402B87E: operator new(unsigned int) (vg_replace_malloc.c:292)
  ==11289==    by 0x807FA75: VOMC::sub_train(std::vector&lt;Letter, std::allocator&lt;Letter&gt; &gt;,    std::vector&lt;Letter, std::allocator&lt;Letter&gt; &gt;) (VOMC.cpp:952)
  </code></pre>
  
  <p>For memory leak -2</p>
  
  <pre><code>==11289==    at 0x402B87E: operator new(unsigned int) (vg_replace_malloc.c:292)
  ==11289==    by 0x807FB16: VOMC::sub_train(std::vector&lt;Letter, std::allocator&lt;Letter&gt; &gt;,     std::vector&lt;Letter, std::allocator&lt;Letter&gt; &gt;) (VOMC.cpp:968)
  </code></pre>
  
  <p>Can I delete these pointers? This would remove my leaks BUT as you can see the pointers are being pushed into a stack, the goal is to maintain them in the stack but remove the leak.</p>
  
  <p>EDIT -1 : adding letter and state definitinons:</p>
  
  <pre><code>class Letter {
  
  public:
      Letter();
      Letter(Helper *helper);
      ~Letter();
  
      void add_note(RawNote r);
      void evaluate_letter(double eigthNoteDuration);
      void setNotePositionAccordingToLetter();
      void empty_notes();
      LetterPattern getPattern() const;
      void setPattern(LetterPattern pattern);
      bool isEmpty();
      vector&lt;RawNote&gt; getRawNotes() const;
      void setRawNotes(vector&lt;RawNote&gt; rawNotes);
  
      bool has_note_no_velocity(RawNote* r1);
      bool has_note_with_velocity(RawNote* r1);
  
  private:
      vector&lt;LetterPattern&gt; *allPossibleNotes;
      vector&lt;RawNote&gt; rawNotes;
      LetterPattern pattern;
  </code></pre>
  
  <p>};</p>
  
  <pre><code>class State {
  public:
  State(); //should not be used, it is only for testing
  State(vector&lt;Letter&gt; letters);
  virtual ~State();
  int GetId() const;
  void SetId(int id);
  vector&lt;Letter&gt; GetLetters() const;
  void SetLetters(vector&lt;Letter&gt; letters);
  void AddLetters(vector&lt;Letter&gt; letters);
  void inc_state(int state_id);
  void print_state_letters();
  bool has_state(int state_id);
  void print_connected_states();
  void print_sorted_states();
  vector&lt;string&gt; get_rhythm_as_string();
  map&lt;int, double&gt; GetConnected_states() const;
  map&lt;int, double&gt; connected_states;
  vector&lt; pair &lt;int, double &gt; &gt; vector_sorted_connected_states;
  void bubblesort_vector_descending(vector&lt; pair &lt;int, double &gt; &gt; *v_sort);
  int get_connected_state_stochastically();
  static int id_generator;
  CustomNumberDist *normal_dist;
  int id_from_file; //only used on load of a file
  private:
  int id;
  vector&lt;Letter&gt; letters;
  };
  </code></pre>