title: Deleting dynamically created component?
body: <p>In the <code>OnCreate</code> event, I have code which adds menu items to a <code>std::vector</code> (for the purposes of this question, it could be buttons, or any other components we create dynamically with the <code>new</code> operator):</p>
  <pre><code>// Iterrate through styles and hints and construct menu
  for (int i=0; i&lt;styleNamesAndHints-&gt;Count; i++) {
      TMenuItem* miTheme = new TMenuItem(miRoot);
      miTheme-&gt;Caption = styleNamesAndHints-&gt;Strings[i].SubString(0, styleNamesAndHints-&gt;Strings[i].Pos(styleNamesAndHints_Delimeter)-1);
      miTheme-&gt;AutoCheck = false;
      miTheme-&gt;AutoHotkeys = maManual;
      miTheme-&gt;RadioItem = false;
      miTheme-&gt;GroupIndex = MENU_GROUP_INDEX_STYLES;
  
      miTheme-&gt;OnClick = &amp;miStyles_Click;
      miTheme-&gt;OnDrawItem = &amp;miStyles_DrawItem;
      miTheme-&gt;OnMeasureItem = &amp;miStyles_MeasureItem;
      miTheme-&gt;ImageIndex = IL_MENU_A_THEME;
      miTheme-&gt;Tag = miStyleArray.size();
  
      miStyleArray.push_back(miTheme);
  
      miRoot-&gt;Add(miTheme);       
  }
  </code></pre>
  <p>Then in the <code>OnDestroy</code> event, we clean up the vector <code>miStyleArray</code>:</p>
  <pre><code>// Clean up miStyleArray vector, delete each element
  while(miStyleArray.empty()==false) {
      delete miStyleArray.back();
      miStyleArray.pop_back();
  }
  </code></pre>
  <p>This code works... I have no errors, everything works perfectly....</p>
  <p>Now, someone told me that code like this could lead to errors and that, I quote, I should not use operator <code>delete</code> on objects that have parent, parent of the object is responsible for destroying and clean up..</p>
  <p>As a matter of fact, the person that told me this is a moderator at a C++Builder forum, and warned me that I could be banned next time for such code! Here is the <a href="https://www.cyberforum.ru/cpp-builder/thread2942359.html" rel="nofollow noreferrer">post link</a>.</p>
  <p>Who is right, who is wrong??</p>