# ScopedPointers can delete the pointer value twice

**URL:** <https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111>\
**Category:** General JUCE discussion\
**Created:** [January 4, 2019, 4:34am UTC](https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111 "2019-01-04T04:34:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [January 4, 2019, 4:34am UTC](https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111/1 "2019-01-04T04:34:17Z")

</div>

ScopedPointers can delete the pointer value twice. Here is a fairly contrived example that reproduces the problem as simply as possible.

```auto
struct Thingy
{
    ~Thingy();
    int cnt = 0;
};

ScopedPointer<Thingy> thingy;

Thingy::~Thingy()
{
    cnt++;
    if (cnt == 2)
        printf ("Deconstructors should only be called once!\n");
    
    thingy.reset();
}

int main (int argc, char* argv[])
{
    thingy.reset (new Thingy());
    thingy.reset();
    
    return 0;
}

```

The issue is that while `void reset (ObjectType* newObject)` moves the pointer to a temp variable and then deletes it, to guard against reentrant calls to `reset`, `void reset()` does not have this guard in place. I think it should be written like this:

```auto
    void reset()
    {
        auto* oldObject = object;
        object = {};
        ContainerDeletePolicy<ObjectType>::destroy (oldObject);
    }

```

Ok, my example is a terrible / bizarre thing to do, but you can get into this situation pretty easily if your deconstructors call listeners. If this really is a bad thing to do then it should give an assert and not crash.

Example project here: [https://github.com/FigBug/juce\_bugs/blob/master/OSP\_Crash/Source/Main.cpp](https://github.com/FigBug/juce_bugs/blob/master/OSP_Crash/Source/Main.cpp)

---

<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 4, 2019, 8:46am UTC](https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111/2 "2019-01-04T08:46:14Z")

</div>

Oh - good catch! I’ll sort that out.

(Best to migrate everything to std::unique\_ptr though!)

---

<div class="post-metadata">

**Author:** ![CrushedPixel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/crushedpixel/32/8654_2.png) [@CrushedPixel](https://forum.juce.com/u/CrushedPixel)\
**Post date:** [January 4, 2019, 2:23pm UTC](https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111/3 "2019-01-04T14:23:21Z")

</div>

Thank you for pinpointing this issue - I’ve had occasional crashes due to this but couldn’t figure out what exactly went wrong.

---

<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 4, 2019, 10:04pm UTC](https://forum.juce.com/t/scopedpointers-can-delete-the-pointer-value-twice/31111/4 "2019-01-04T22:04:52Z")

</div>

Is this problem the result of that ScopedPointer not being owned by any particular object or being given the scope of some function or other object, and just being a free global?

```auto
Thingy::~Thingy()
{
    cnt++;
    if (cnt == 2)
        printf ("Deconstructors should only be called once!\n");
    
    thingy.reset();
}

```

the Thingy destructor is resetting a global variable via `thingy.reset()`.
