# Beginner: how to create component using 'new'

**URL:** <https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848>\
**Category:** Getting Started\
**Created:** [February 6, 2024, 10:01am UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848 "2024-02-06T10:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![freekuency](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@freekuency](https://forum.juce.com/u/freekuency)\
**Post date:** [February 6, 2024, 10:01am UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848/1 "2024-02-06T10:01:52Z")

</div>

This is a very beginners question and I’m feeling ashamed I can not figure this out.  
When I create a slider which is a member of my main component, all is fine.  
But when I create this slider using the ‘new’ my application crashes.

This is what I do:

In MainComponent.h:

```auto
private:
  Slider* frequencySlider;

```

In MainComponent.cpp:

```auto
MainComponent::MainComponent()
{
    setSize (800, 600);

    frequencySlider = new Slider("SliderName");
    addAndMakeVisible (frequencySlider);
    frequencySlider->setRange (50, 5000.0);
    frequencySlider->setTextValueSuffix (" Hz"); 
}

void MainComponent::resized()
{
        auto sliderLeft = 120;
        frequencySlider->setBounds (sliderLeft, 20, getWidth() - sliderLeft - 10, 20);
}

```

The application crashes on the setBounds() in resize().  
Any idea what I do wrong?

---

<div class="post-metadata">

**Author:** ![baramgb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baramgb/32/12822_2.png) [@baramgb](https://forum.juce.com/u/baramgb)\
**Post date:** [February 6, 2024, 10:15am UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848/2 "2024-02-06T10:15:56Z")

</div>

You should use ‘std::unique\_ptr\< juce::Slider\>’ for this. And ‘frequencySlider = std::make\_unique\< juce::Slider\>(“slider name”);’

But the best way is to avoid pointers on creating components. You only should use pointers if you really need to.

---

<div class="post-metadata">

**Author:** ![chrhaase](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chrhaase/32/12648_2.png) [@chrhaase](https://forum.juce.com/u/chrhaase)\
**Post date:** [February 6, 2024, 10:40am UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848/3 "2024-02-06T10:40:00Z")

</div>

You’re calling `setSize` (which results in a call to `MainComponent::resized()`) before creating the slider, so you’re calling `setBounds` on the uninitialised `Slider*`, hence the crash.

Moving `setSize` to the bottom of your constructor should avoid crash.

---

<div class="post-metadata">

**Author:** ![freekuency](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@freekuency](https://forum.juce.com/u/freekuency)\
**Post date:** [February 6, 2024, 12:44pm UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848/4 "2024-02-06T12:44:52Z")

</div>

Ahh, I knew I did something stupid. Thanks!

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [February 6, 2024, 5:05pm UTC](https://forum.juce.com/t/beginner-how-to-create-component-using-new/59848/5 "2024-02-06T17:05:53Z")

</div>

most likely an architectural problem or a language misunderstanding.

if your frequency parameter should always exist it should just be an object instead of a pointer.

```auto
Slider frequencySlider;

MainComponent::MainComponent() :
    frequencySlider("SliderName")
{
    addAndMakeVisible(frequencySlider);
    //..
    setSize(800, 600);
}

```

if you need it to be dynamic you can either do that by calling setVisible() on it when needed or by indeed making it a pointer, in which case unique\_ptr is the best choice. it would look like this:

```auto
std::unique_ptr<Slider> frequencySlider;

MainComponent::MainComponent() :
    frequencySlider(std::make_unique<Slider>("SliderName"))
{
    addAndMakeVisible(*frequencySlider);
    //..

```

unique\_ptr is useful if you wanna swap the component with another one later, or if it can be components of different types that derive from the pointer’s base class, or as part of a vector to enable a dynamic amount of components of some type.  
careful with the methods setRange(), setTextValueSuffix(), and setValue(). If you ever make plugins these will become obsolete, because the so called parameter attachments tell the slider its properties already. beginners often try to keep it nice and simple by not immediatly jumping into a plugin project, but in turn learn some things that will be misleading later on in that process
