'Deleting dynamically created component?

In the OnCreate event, I have code which adds menu items to a std::vector (for the purposes of this question, it could be buttons, or any other components we create dynamically with the new operator):

// Iterrate through styles and hints and construct menu
for (int i=0; i<styleNamesAndHints->Count; i++) {
    TMenuItem* miTheme = new TMenuItem(miRoot);
    miTheme->Caption = styleNamesAndHints->Strings[i].SubString(0, styleNamesAndHints->Strings[i].Pos(styleNamesAndHints_Delimeter)-1);
    miTheme->AutoCheck = false;
    miTheme->AutoHotkeys = maManual;
    miTheme->RadioItem = false;
    miTheme->GroupIndex = MENU_GROUP_INDEX_STYLES;

    miTheme->OnClick = &miStyles_Click;
    miTheme->OnDrawItem = &miStyles_DrawItem;
    miTheme->OnMeasureItem = &miStyles_MeasureItem;
    miTheme->ImageIndex = IL_MENU_A_THEME;
    miTheme->Tag = miStyleArray.size();

    miStyleArray.push_back(miTheme);

    miRoot->Add(miTheme);       
}

Then in the OnDestroy event, we clean up the vector miStyleArray:

// Clean up miStyleArray vector, delete each element
while(miStyleArray.empty()==false) {
    delete miStyleArray.back();
    miStyleArray.pop_back();
}

This code works... I have no errors, everything works perfectly....

Now, someone told me that code like this could lead to errors and that, I quote, I should not use operator delete on objects that have parent, parent of the object is responsible for destroying and clean up..

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 post link.

Who is right, who is wrong??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source