Delete child component from inside component?

Hey - I wonder if this is too dodgy…
I have a component that makes children, and holds the pointer:
std::unique_ptr controls;
Then makes new with .reset( new etc) then makes visible.

When each child control has done its job - currently - I call a parent function (from the child’s buttonlistener) which just makes a new component, resetting the controls.
To my surprise, that works… but I’m realizing that I never did removeChildComponent() on it. The docs seem to indicate I can do that?
How bad is this? Memory leak? Or are components smart enough to deal with it?

Background - trying to use Juce in a RAD manner, doing the obvious and quick thing, not a whole bunch of architecting

Thanks,

Bruce

If anyone wants to join me in the rabbit hole…
https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
The language appears to support it in general, and I meet those rules, so it is down to what is happening with that pointer class…

Seeing as how components just store their children using an array of raw pointers I think you’ll want to call removeChildComponent() before deleting the component. Otherwise you may have a dangling pointer (via this internal array) to the child that was deleted. Have you gotten a crash or anything?

Is this just a single instance of std::unique_ptr<Component> or one per control?

You’d think so, but docs for removeChildComponent():
“Note that removing a child will not delete it! But it’s ok to delete a component without first removing it - doing so will automatically remove it and send out the appropriate notifications before the deletion completes.”

No crashes, nope. No reported leaks.

I’m using this like a poor-man’s tab control. Working so far.

Huh, wasn’t aware that was a thing :slight_smile:

Here it is:

So yep, deleting children without removing them is totally safe!

Cool, huh‽
So, I’m thinking my nasty tab panel by pointer re-assignment is a go!
Hope I don’t have to animate it!

Thanks for looking with me.

1 Like