# SharedResourcePointer

**URL:** <https://forum.juce.com/t/sharedresourcepointer/12699>\
**Category:** General JUCE discussion\
**Created:** [April 14, 2014, 12:26pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699 "2014-04-14T12:26:25Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [April 14, 2014, 12:26pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/1 "2014-04-14T12:26:25Z")

</div>

I've just added a new class&nbsp;SharedResourcePointer, which some of you may find handy.

Basically, it's a simple smart-pointer that automatically manages a shared static instance of an object. The interesting bit about it is that it's ref-counted, so that when all of your&nbsp;SharedResourcePointers go out of scope, the shared resource is also freed. That makes it a bit like a singleton, but more dynamic, and it means that your shared object will get deleted before shutdown without using DeletedAtShutdown, or relying on the runtime's statics being deleted.

In particular, this may be a good thing for plugins, where you don't really want to use a static or singleton, but have shared things, e.g. pre-calculated values or shared graphics, which you want to share between instances of your plugin.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [April 14, 2014, 2:23pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/2 "2014-04-14T14:23:06Z")

</div>

Nice addition! I think that most of us developing plug-ins already have wrapped their own solution for this kind of topic, it's good that it gets supported by the library now :)

One suggestion, though: as you did for some other such classes, I think that a small code example in the class documentation would greatly help understand what's the intended use for the class

---

<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:** [April 14, 2014, 3:03pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/3 "2014-04-14T15:03:47Z")

</div>

Yes, it's a pattern that I've found myself reinventing multiple times over the years without realising it. Will add a code example when I get a moment, and there are probably quite a few bits of existing code&nbsp;in the library which&nbsp;I'll be able to simplify&nbsp;with it.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [April 14, 2014, 3:20pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/4 "2014-04-14T15:20:31Z")

</div>

\*cheers from the crowd\*

---

<div class="post-metadata">

**Author:** ![Peter\_Emanuel\_Roos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peter_emanuel_roos/32/189_2.png) [@Peter\_Emanuel\_Roos](https://forum.juce.com/u/Peter_Emanuel_Roos)\
**Post date:** [April 14, 2014, 3:49pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/5 "2014-04-14T15:49:22Z")

</div>

Very nice!&nbsp;

And if someone can point me to programming solutions to share initialisation code and resources between plugin instances, I would be very grateful!&nbsp;

---

<div class="post-metadata">

**Author:** ![joanpuigsanz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/joanpuigsanz/32/183_2.png) [@joanpuigsanz](https://forum.juce.com/u/joanpuigsanz)\
**Post date:** [April 18, 2014, 3:28pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/6 "2014-04-18T15:28:33Z")

</div>

Can you show us an exapmle with a static variable?

---

<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:** [April 18, 2014, 3:39pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/7 "2014-04-18T15:39:15Z")

</div>

There's example code in the class's documentation.

---

<div class="post-metadata">

**Author:** ![jpo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jpo/32/503_2.png) [@jpo](https://forum.juce.com/u/jpo)\
**Post date:** [April 19, 2014, 10:40am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/8 "2014-04-19T10:40:00Z")

</div>

Isn't there a potential alignment issue with this code ? :

static SharedObjectHolder& getSharedObjectHolder() noexcept  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static char holder [sizeof (SharedObjectHolder)] = { 0 };  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \*reinterpret\_cast\<SharedObjectHolder\*\> (holder);  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;

(since holder is not required to have the alignment needed by the SharedObjectHolder struct)

---

<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:** [April 19, 2014, 7:30pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/9 "2014-04-19T19:30:14Z")

</div>

Why would it matter whether or not it's aligned?

(Though TBH I'd be very surprised if any compiler didn't align a 24-byte static block)

---

<div class="post-metadata">

**Author:** ![jpo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jpo/32/503_2.png) [@jpo](https://forum.juce.com/u/jpo)\
**Post date:** [April 20, 2014, 8:22am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/10 "2014-04-20T08:22:08Z")

</div>

It would cause a crash on CPUs that do not handle word access on non-aligned address (x86 have no problems with that) -- according to http://www.ibm.com/developerworks/library/pa-dalign/ there could also be issues also with the atomical memory access of the spinlock if it is not correctly aligned

&nbsp;

---

<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:** [April 20, 2014, 8:27am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/11 "2014-04-20T08:27:03Z")

</div>

Hmm. Ok, fair enough, I had forgotten about ARM.&nbsp;Even so, I'm sceptical that any compiler would actually fail to align a block like that, and I vaguely remember hearing that the C++11 spec explicitly says that larger blocks that aren't part of a larger structure&nbsp;should be aligned (I could be wrong about that..). But, since it's easy enough for me to make sure it's aligned, I will do!

---

<div class="post-metadata">

**Author:** ![pauloesteban](https://avatars.discourse-cdn.com/v4/letter/p/a5b964/32.png) [@pauloesteban](https://forum.juce.com/u/pauloesteban)\
**Post date:** [April 19, 2019, 2:24am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/12 "2019-04-19T02:24:20Z")

</div>

Hello everyone,

I am bit lost in using a SharedResourcePointer. Is there any code you have to see how it works between multiple instances of an application or plugin?

Cheers,  
Paulo

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [April 19, 2019, 3:01am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/13 "2019-04-19T03:01:09Z")

</div>

As far as applications go, there isn’t much about it. Applications running in different processes will have their own instances. It can still be useful if you want your application to have some object that can be accessed everywhere without recreating it each time it is needed. (In the same process.)

With plugins it is more interesting. Each plugin instance will be using the same object when you use it via SharedResourcePointer. You would typically have it as a member of your AudioProcessor subclass. The obvious candidates for use are for example AudioFormatManager and AudioThumbNailCache. But you will need to subclass those, SharedResourcePointer needs the held object to have a default constructor. (AudioFormatManager has a default constructor but you will probably want the used instance to have its registerBasicFormats() method called before use.)

---

<div class="post-metadata">

**Author:** ![ttg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ttg/32/14456_2.png) [@ttg](https://forum.juce.com/u/ttg)\
**Post date:** [April 19, 2019, 5:55am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/14 "2019-04-19T05:55:02Z")

</div>

I’m far than being a proper cpp coder but as JUCE switches to c++11, shouldn’t ‘std::shared\_ptr’ be enough?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 19, 2019, 9:24am UTC](https://forum.juce.com/t/sharedresourcepointer/12699/15 "2019-04-19T09:24:00Z")

</div>

The shared\_ptr needs to be created somewhere and you need access to it.  
But the SharedResourcePointer will grab the one and only instance out of thin air (similar to a singleton), IIUC. Just with automatic disposal when no reference is held, opposed to the singleton, that will be cleaned up when the app or the plugin is closed.

(just realise, what happens if you use a Singleton in a plugin? probably a bad idea anyway, since statics are problematic)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [April 19, 2019, 3:33pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/16 "2019-04-19T15:33:32Z")

</div>

> [@daniel](#):
>
> (just realise, what happens if you use a Singleton in a plugin? probably a bad idea anyway, since statics are problematic)

It is a bad idea if one isn’t careful with how they are used, but they can still be useful in some limited cases.

The fear about statics and globals in plugins mostly rose when multicore CPUs happened and the audio processing actually started happening simultaneously from multiple threads. But for example for GUI things, statics and globals _likely_ can be used without too many issues, as long as one realizes each plugin instance will be using the same variables/objects. There should always be a _good_ reason to use globals and statics. For example, convenience of getting access to some desired objects/variables usually isn’t one.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 19, 2019, 3:39pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/17 "2019-04-19T15:39:14Z")

</div>

I think the minimum damage they do is that they leak by design. There is no way they are destroyed automatically, only once the host is quit.  
So it’s not that anything will crash, when statics are used, but I think it is no mistake to avoid them.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [April 19, 2019, 3:40pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/18 "2019-04-19T15:40:40Z")

</div>

Well, that’s why you want to use SharedResourcePointer and not statics/globals directly… 😉

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 19, 2019, 3:42pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/19 "2019-04-19T15:42:12Z")

</div>

Indeed, because the SharedResourcesPointer doesn’t leak, does it?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [April 19, 2019, 3:45pm UTC](https://forum.juce.com/t/sharedresourcepointer/12699/20 "2019-04-19T15:45:53Z")

</div>

It doesn’t, it keeps track of the use count of the object, when it goes to zero, it is deleted. If the object is later needed again, it is recreated using the class’s default constructor. The requirement to have the default constructor is a bit annoying and could actually be fixed with some C++11 and later magic, though…

[Next page](https://forum.juce.com/t/sharedresourcepointer/12699.md?page=2)
