# Use of ScopedPointer in synthesiser demo (noob question)

**URL:** <https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344>\
**Category:** General JUCE discussion\
**Created:** [February 22, 2015, 10:05pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344 "2015-02-22T22:05:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![willbak](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@willbak](https://forum.juce.com/u/willbak)\
**Post date:** [February 22, 2015, 10:05pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/1 "2015-02-22T22:05:01Z")

</div>

Hi,

I am trying to learn a little about Juce, but I am no expert in programming. I do not understand&nbsp;exactly the use of the ScopedPointer below:

(that is from AudioSynthesiserDemo)

```
 void setUsingSampledSound()
    {
        WavAudioFormat wavFormat;
        ScopedPointer<AudioFormatReader> audioReader (wavFormat.createReaderFor (new MemoryInputStream (BinaryData::cello_wav,                                                                                          BinaryData::cello_wavSize,
false),
true));
        BigInteger allNotes;
        allNotes.setRange (0, 128, true);
        synth.clearSounds();
        synth.addSound (new SamplerSound ("demo sound",
                                          *audioReader,
                                          allNotes,
                                          74,   // root midi note
                                          0.1,  // attack time
                                          0.1,  // release time
                                          10.0  // maximum sample length
                                          ));
    }
```

Is it absolutely necessary ? Does it have to do with the dynamic allocation of MemoryInputStream ? Does it have to do with its use in SamplerSound constructor ?

Thank you in advance for your explanation

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [February 23, 2015, 1:52pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/2 "2015-02-23T13:52:00Z")

</div>

Have a read about the C++ operators new and delete:&nbsp;http://www.cplusplus.com/doc/tutorial/dynamic/ .

Also, feel free to read up on scope:&nbsp;http://en.wikipedia.org/wiki/Scope\_%28computer\_science%29  
  
In short, a ScopedPointer, when it goes out of scope,&nbsp;simply handles deleting the object it has ownership over. This makes use of the RAII idiom to avoid accidentally leaking memory (i.e.: Forgetting to "delete" a heap object).

I suggest reading the description of the class for more information:&nbsp;https://www.juce.com/api/classScopedPointer.html#details

It's not necessary to use this, but it is suggested to use this approach since it's modern practice in C++.&nbsp;JUCE provides ScopedPointer because not all platforms implement the new C++11 stuff.&nbsp;Check out:&nbsp;http://en.cppreference.com/w/cpp/memory/auto\_ptr and&nbsp;http://en.cppreference.com/w/cpp/memory/unique\_ptr .

This type of thing has no direct relation to any class, it's a memory handling technique.

---

<div class="post-metadata">

**Author:** ![willbak](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@willbak](https://forum.juce.com/u/willbak)\
**Post date:** [February 23, 2015, 5:40pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/3 "2015-02-23T17:40:22Z")

</div>

Thank you for your answer. I know it has to do mainly with C++ functionning rather than Juce. But I was wondering if the context here made it necessary.

In the following code, in AudioPlaybackDemo, there is a simple pointer:

```
 transportSource.stop();
        transportSource.setSource (nullptr);
        currentAudioFileSource = nullptr;
        AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
        if (reader != nullptr)
        {...}
```

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [February 23, 2015, 6:32pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/4 "2015-02-23T18:32:10Z")

</div>

1. In the first case it is a classic RAII approach to manage the release of the object returned.

2. In the second case the ownership of the object is passed to another object later in the code.

```
currentAudioFileSource = new AudioFormatReaderSource (reader, true);
```

And so a ScopedPointer would result in deleted the object twice.

---

<div class="post-metadata">

**Author:** ![willbak](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@willbak](https://forum.juce.com/u/willbak)\
**Post date:** [February 23, 2015, 6:32pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/5 "2015-02-23T18:32:12Z")

</div>

Oh, I see. Thank you for your answer.

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [February 23, 2015, 6:41pm UTC](https://forum.juce.com/t/use-of-scopedpointer-in-synthesiser-demo-noob-question/14344/6 "2015-02-23T18:41:39Z")

</div>

As jules collects black belts in RAII there MUST be a valuable reason somewhere to let a lonely pointer 😉
