# Some behaviour with ScopedPointer I don't understand

**URL:** <https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901>\
**Category:** General JUCE discussion\
**Created:** [May 26, 2014, 3:51pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901 "2014-05-26T15:51:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbeguec](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@fbeguec](https://forum.juce.com/u/fbeguec)\
**Post date:** [May 26, 2014, 3:51pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/1 "2014-05-26T15:51:19Z")

</div>

Hi all

I'm on VS2010, and have the following code:

```
const bool bStereo = (m_uNbChannels == 2);
const bool b51Srnd = (m_uNbChannels == 6);ScopedPointer\<float\*\> ptrProcBuffer = new float\*[m\_uNbChannels];m\_AnalyzeFilter.ProcessBlock(bStereo? ptrProcBuffer : nullptr, b51Srnd? ptrProcBuffer : nullptr, kBlockLength);
```

&nbsp;

with&nbsp;

```
bool ProcessBlock(float\*\* const ppfStereoData, float\*\* const ppf51SrndData, const unsigned int uBlockSize);
```

Well, when calling the processBlock function I would expect the scopedpointer to call its float\*\* cast operator then the .get() method to be called, nothing else.&nbsp;

But instead, a new ScopedPointer object is constructed from the existing one, and then the original one is reset...

why?

my only workaround so far is to call the .get() method explicitly like:

```
m_AnalyzeFilter.ProcessBlock(bStereo? ptrProcBuffer.get() : nullptr, b51Srnd? ptrProcBuffer.get() : nullptr, kBlockLength);
```

Thanks for advise

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [May 26, 2014, 4:01pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/2 "2014-05-26T16:01:00Z")

</div>

In any case your code is wrong as&nbsp;  
ScopedPointer will not call delete [] leading to either leak or crash

&nbsp;

use std::vector and &myVector[0] IMHO

---

<div class="post-metadata">

**Author:** ![fbeguec](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@fbeguec](https://forum.juce.com/u/fbeguec)\
**Post date:** [May 26, 2014, 4:18pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/3 "2014-05-26T16:18:23Z")

</div>

oh yes true, I always do this error, should use my own scoped pointer class

&nbsp;

thanks

---

<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:** [May 26, 2014, 4:28pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/4 "2014-05-26T16:28:47Z")

</div>

No... you shouldn't use _any_ kind of scoped pointer for holding&nbsp;arrays.

Use std::vector, or Array. Or since I presume you're trying to hold a set of audio channels, use a AudioSampleBuffer, which is designed for exactly that&nbsp;purpose.

---

<div class="post-metadata">

**Author:** ![fbeguec](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@fbeguec](https://forum.juce.com/u/fbeguec)\
**Post date:** [May 26, 2014, 4:35pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/5 "2014-05-26T16:35:25Z")

</div>

ok my previous mistake apart,&nbsp;still..&nbsp;

using a custom ScopedPointer class (where you can specify the deleter to be free, delete, delete[] or CoTaskMemFree) I still get exactly the same behaviour.. and I'm still curious about it..

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [May 26, 2014, 4:57pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/6 "2014-05-26T16:57:28Z")

</div>

isn't it because the&nbsp;ScopedPointer constructor is not declared "explicit" ?

---

<div class="post-metadata">

**Author:** ![fbeguec](https://avatars.discourse-cdn.com/v4/letter/f/7993a0/32.png) [@fbeguec](https://forum.juce.com/u/fbeguec)\
**Post date:** [May 27, 2014, 3:47pm UTC](https://forum.juce.com/t/some-behaviour-with-scopedpointer-i-dont-understand/12901/7 "2014-05-27T15:47:04Z")

</div>

mm maybe, have to give it a try
