# Close Button Crash!

**URL:** <https://forum.juce.com/t/close-button-crash/12603>\
**Category:** General JUCE discussion\
**Created:** [March 26, 2014, 8:35pm UTC](https://forum.juce.com/t/close-button-crash/12603 "2014-03-26T20:35:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vicente\_Carrasco](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vicente\_Carrasco](https://forum.juce.com/u/Vicente_Carrasco)\
**Post date:** [March 26, 2014, 8:35pm UTC](https://forum.juce.com/t/close-button-crash/12603/1 "2014-03-26T20:35:54Z")

</div>

Hello!

I have a problem: when I click on close button on the main window app crashes...

Xcode shows me a SIGABRT signal at menuBar destructor... Any suggestion?

&nbsp;

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [March 27, 2014, 4:47am UTC](https://forum.juce.com/t/close-button-crash/12603/2 "2014-03-27T04:47:09Z")

</div>

How about a "setMenuBar(nullptr);" in your destructor?

---

<div class="post-metadata">

**Author:** ![Vicente\_Carrasco](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vicente\_Carrasco](https://forum.juce.com/u/Vicente_Carrasco)\
**Post date:** [March 27, 2014, 8:57am UTC](https://forum.juce.com/t/close-button-crash/12603/3 "2014-03-27T08:57:22Z")

</div>

Thanks Rory,

but I've tried with:

```
**_aud\_MenuBar::~aud\_MenuBar()_****_{_ **** _deleteAllChildren();_ **** _menuBar.MenuBarComponent::setModel(nullptr);_ ****_}_**
```

and the issue continues:&nbsp;

```
malloc: *** error for object 0x10181d9f8: pointer being freed was not allocated
```

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [March 27, 2014, 10:59am UTC](https://forum.juce.com/t/close-button-crash/12603/4 "2014-03-27T10:59:59Z")

</div>

Actually, setMenuBar() is a member of the DocumentWindow class.&nbsp; I take it your menu bar is being used in some sort of resizable window?

---

<div class="post-metadata">

**Author:** ![Vicente\_Carrasco](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vicente\_Carrasco](https://forum.juce.com/u/Vicente_Carrasco)\
**Post date:** [March 27, 2014, 11:39am UTC](https://forum.juce.com/t/close-button-crash/12603/5 "2014-03-27T11:39:09Z")

</div>

That's correct. It's being used in a resizable window

---

<div class="post-metadata">

**Author:** ![Vicente\_Carrasco](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vicente\_Carrasco](https://forum.juce.com/u/Vicente_Carrasco)\
**Post date:** [March 27, 2014, 11:39am UTC](https://forum.juce.com/t/close-button-crash/12603/6 "2014-03-27T11:39:09Z")

</div>

That's correct. It's being used in a resizable window

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [March 27, 2014, 2:25pm UTC](https://forum.juce.com/t/close-button-crash/12603/7 "2014-03-27T14:25:53Z")

</div>

And you add it by calling "setMenuBar(this, height)" in the constructor. So in the destructor for that window call "setMenuBar(0)".&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![Vicente\_Carrasco](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vicente\_Carrasco](https://forum.juce.com/u/Vicente_Carrasco)\
**Post date:** [March 27, 2014, 2:55pm UTC](https://forum.juce.com/t/close-button-crash/12603/8 "2014-03-27T14:55:05Z")

</div>

I fix the problem deleting this line in the destructor:

```
deleteAllChildren();
```

Thanks!

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [January 28, 2015, 9:00am UTC](https://forum.juce.com/t/close-button-crash/12603/9 "2015-01-28T09:00:00Z")

</div>

I'm running into the same problem with the MidiKeyboardComponent class. &nbsp; Every example I've seen for the MidiKeyboardComponent shows deleteAllChildren() being used in the destructor for the class that's holding the MidiKeyboardComponent. &nbsp; There's also no indication if the class instance of MidiKeyboardComponent should be a pointer initialized via 'new' or explicitly initialized in the class constructor. &nbsp; &nbsp;The first tutorial for JUCE (http://lubyk.org/en/software/mimas/document171.pdf) shows how to use components as pointer variables, and makes use of the deleteAllChildren() method. &nbsp;

&nbsp;

Are we supposed to follow this tutorial's demonstration&nbsp;for using ALL&nbsp;components offering in JUCE?

&nbsp;

i.e. always use&nbsp;

```
class SubComponent : public Component

{

public: 

  SubComponent();

  ~SubComponent();

  MidiKeyboardState* state;

  MidiKeyboardComponent* keyboard;

  void paint( Graphics&) override;

  void resized() override;

};

SubComponent::SubComponent()

{

  state = new MidiKeyboardState();

  keyboard = new MidiKeyboardComponent( &state, MidiKeyboardComponent::horizontalKeyboard);

  addAndMakeVisible( keyboard );

}

SubComponent::~SubComponent()

{

  deleteAllChildren();

}
```

&nbsp;

and never use:

```
​
class SubComponent : public Component

{

  public: SubComponent();

  ~SubComponent();

  MidiKeyboardState state;

  MidiKeyboardComponent keyboard;

  void paint( Graphics&) override;

  void resized() override;

};

SubComponent::SubComponent()

: keyboard( state, MidiKeyboardComponent::horizontalKeyboard);

{ 

  addAndMakeVisible( keyboard );

}

SubComponent::~SubComponent()

{ 
  //deleteAllChildren(); 
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 28, 2015, 9:10am UTC](https://forum.juce.com/t/close-button-crash/12603/10 "2015-01-28T09:10:52Z")

</div>

Please don't use deleteAllChildren()!

I don't use it in my own example code, but obviously I can't stop other people putting it in their tutorials. It's not a robust&nbsp;way to manage object lifetimes - much better to manage&nbsp;the component as a normal member variable, or use a ScopedPointer to it if you really need to create it on the heap.
