Missing removeChildComponent call in demo app?

I’m trying to analyze the juce demo application right now.
Thereby I do not understand the following:

The method ContentComp::showDemo of the juce looks like this:

    void showDemo (Component* demoComp, const char* sourceCodeText)
    {
        delete currentDemo;
        currentDemo = demoComp;

        addAndMakeVisible (currentDemo);
        resized();

        demoSourceCodeText = sourceCodeText;
    }

But shouldn’t it be necessairy to call removeChildComponent(currentDemo);
before deleting the child component? E.g.:

    void showDemo (Component* demoComp)
    {
		if (currentDemo)
		{
			removeChildComponent(currentDemo);
			delete currentDemo;
		}
        currentDemo = demoComp;

        addAndMakeVisible (currentDemo);
        resized();
    }

regards
jan

No need - if you delete a component it automatically removes itself from its parent.

also, you could avoid to check if currentDemo exists at all…
instead of:

if (currentDemo) { removeChildComponent(currentDemo); delete currentDemo; }

you could do:

but remember to initialize currentDemo (0) in your constructor initializer list.

Great, I would never expect such a reasonable behaviour from children :slight_smile: