# Scoped pointers versus conventional pointers

**URL:** <https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018>\
**Category:** General JUCE discussion\
**Created:** [June 14, 2011, 9:38am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018 "2011-06-14T09:38:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [June 14, 2011, 9:38am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/1 "2011-06-14T09:38:29Z")

</div>

Is there any reason to ever use a conventional pointer over a scoped one? I have a project that has a GUI editor of sorts. It uses Haydxn’s layout editor class\* which I know works fine. If I use a scoped pointer for this class, as well as my main panel class, I get an access violation the second time the destructor for my plugin editor is called and VS takes me to the following in juce\_amalgameted.h:

`/** Destructor.
This will delete the object that this ScopedPointer currently refers to.
*/
inline ~ScopedPointer() { delete object; }`

If I use conventional pointers and call delete in the destructor everything works fine. I know it’s a bit of a newbie question but can anyone explain to me why this might be happening? I’ve been very careful to use scoped pointers everywhere in my code, and I can’t see why they cause problems in this instance.

\*[http://www.rawmaterialsoftware.com/viewtopic.php?f=6&t=2635&hilit=layout+editor](http://www.rawmaterialsoftware.com/viewtopic.php?f=6&t=2635&hilit=layout+editor)

---

<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:** [June 14, 2011, 10:03am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/2 "2011-06-14T10:03:06Z")

</div>

> [@](#):
>
> Is there any reason to ever use a conventional pointer over a scoped one?

Well yes - you should use a conventional pointer if you _don’t_ want your object to be deleted when it goes out of scope!

You problem just sounds like an object is being deleted twice - maybe you create two ScopedPointers to the same object? Stick some breakpoints in your destructors and you should be able to catch it.

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [June 14, 2011, 10:45am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/3 "2011-06-14T10:45:33Z")

</div>

Thanks Jules. I can’t think of any reasons for not wanting my objects to get deleted when they go out of scope? Anyway, I’ll persevere with the scoped pointers approach and see if I can locate the problem.

---

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [June 14, 2011, 10:56am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/4 "2011-06-14T10:56:28Z")

</div>

The second time the destructor is called? Just put a breakpoint into your destructor and see what is destroying it the first time 🙂

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [June 14, 2011, 11:55am UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/5 "2011-06-14T11:55:48Z")

</div>

Found the problem. I think it’s ok now. I was just confused as to why it would work with regular pointers and not scoped ones. I think I’m over the worst now!

---

<div class="post-metadata">

**Author:** ![tomswirly](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tomswirly/32/236_2.png) [@tomswirly](https://forum.juce.com/u/tomswirly)\
**Post date:** [June 14, 2011, 11:23pm UTC](https://forum.juce.com/t/scoped-pointers-versus-conventional-pointers/7018/6 "2011-06-14T23:23:19Z")

</div>

The reason to use a conventional pointer is exactly when you don’t own the item. For example, many of my top level items have a pointer to my class Instance but Instance is the last thing to be deleted so none of the references are scoped pointers?

I think the question is more - should you ever call delete? And the answer is basically no. Always used scoped pointers or ref counted pointers or shared pointers…
