# Delete child component from inside component?

**URL:** <https://forum.juce.com/t/delete-child-component-from-inside-component/32910>\
**Category:** General JUCE discussion\
**Created:** [April 17, 2019, 9:53pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910 "2019-04-17T21:53:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucewheaton](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@brucewheaton](https://forum.juce.com/u/brucewheaton)\
**Post date:** [April 17, 2019, 9:53pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/1 "2019-04-17T21:53:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brucewheaton](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@brucewheaton](https://forum.juce.com/u/brucewheaton)\
**Post date:** [April 17, 2019, 10:00pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/2 "2019-04-17T22:00:45Z")

</div>

If anyone wants to join me in the rabbit hole…  
[https://isocpp.org/wiki/faq/freestore-mgmt#delete-this](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…

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [April 17, 2019, 10:14pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/3 "2019-04-17T22:14:59Z")

</div>

> [@brucewheaton](#):
>
> but I’m realizing that I never did removeChildComponent()

Seeing as how components just store their children using [an array of raw pointers](https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_gui_basics/components/juce_Component.h#L2276) 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?

> [@brucewheaton](#):
>
> I have a component that makes children, and holds the pointer:  
> std::unique\_ptr controls;

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

---

<div class="post-metadata">

**Author:** ![brucewheaton](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@brucewheaton](https://forum.juce.com/u/brucewheaton)\
**Post date:** [April 17, 2019, 11:37pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/4 "2019-04-17T23:37:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [April 17, 2019, 11:41pm UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/5 "2019-04-17T23:41:11Z")

</div>

Huh, wasn’t aware that was a thing 🙂

Here it is:

> <https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_gui_basics/components/juce_Component.cpp#L475>

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

---

<div class="post-metadata">

**Author:** ![brucewheaton](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@brucewheaton](https://forum.juce.com/u/brucewheaton)\
**Post date:** [April 18, 2019, 4:20am UTC](https://forum.juce.com/t/delete-child-component-from-inside-component/32910/6 "2019-04-18T04:20:18Z")

</div>

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.
