# Components on the heap

**URL:** <https://forum.juce.com/t/components-on-the-heap/11436>\
**Category:** General JUCE discussion\
**Created:** [September 9, 2013, 8:35pm UTC](https://forum.juce.com/t/components-on-the-heap/11436 "2013-09-09T20:35:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/samuel/32/21_2.png) [@samuel](https://forum.juce.com/u/samuel)\
**Post date:** [September 9, 2013, 8:35pm UTC](https://forum.juce.com/t/components-on-the-heap/11436/1 "2013-09-09T20:35:55Z")

</div>

Is it good practise to create component objects on the heap instead of the stack?

If so: Why?

&nbsp;

In the WidgetsDemo.cpp of the JuceDemo application, all the component objects I encountered are created on the heap. The same happen to all the component objects created by the GUI Editor of the Introjucer.

Does the advice from http://www.juce.com/documentation/coding-standards

> If a local variable can be allocated on the stack rather than the heap, then always do so.

only hold for variables and not for (Component) objects?

---

<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:** [September 9, 2013, 8:58pm UTC](https://forum.juce.com/t/components-on-the-heap/11436/2 "2013-09-09T20:58:04Z")

</div>

The only time it'd be possible to use a local stack variable for&nbsp;a component would be if you create one and then&nbsp;run&nbsp;a modal loop -&nbsp;but modal loops are Very Bad, so you shouldn't be doing that kind of thing.

---

<div class="post-metadata">

**Author:** ![samuel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/samuel/32/21_2.png) [@samuel](https://forum.juce.com/u/samuel)\
**Post date:** [September 9, 2013, 9:26pm UTC](https://forum.juce.com/t/components-on-the-heap/11436/3 "2013-09-09T21:26:43Z")

</div>

Guess I haven't expressed myself very well.

What is the advantage of

```
class MainComponent : public Component
{

public:

    MainComponent
    {
        textButton = new TextButton ("push me");
        addAndMakeVisible (textButton);
    }

    resized()
    {
        textButton->setBounds (0, 0, 80, 30);
    }

private:

    ScopedPointer<TextButton> textButton;
};
```

compared to

```
class MainComponent : public Component
{

public:

    MainComponent
      : textButton ("push me")
    {
        addAndMakeVisible (&textButton);
    }

    resized()
    {
        textButton.setBounds (0, 0, 80, 30);
    }

private:

    TextButton textButton;
};
```

&nbsp;

?

---

<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:** [September 9, 2013, 9:30pm UTC](https://forum.juce.com/t/components-on-the-heap/11436/4 "2013-09-09T21:30:55Z")

</div>

That's not on the stack, it's a member variable.

But yes:&nbsp;if it's possible to use an in-place&nbsp;member rather than a ScopedPointer, you should do so. Mainly because it makes your&nbsp;syntax cleaner, but it's&nbsp;marginally faster + more compact too.

---

<div class="post-metadata">

**Author:** ![samuel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/samuel/32/21_2.png) [@samuel](https://forum.juce.com/u/samuel)\
**Post date:** [September 9, 2013, 9:56pm UTC](https://forum.juce.com/t/components-on-the-heap/11436/5 "2013-09-09T21:56:58Z")

</div>

Thank you Jules for this insight!

Sorry for my wrong posed question.
