Sharing an object in different plugins instances question

Hi,

I have few unique objects (like keyboard configuration, lookandfeel settings, etc) that I want to share between all opened VST/AU plugins. I thought doing them Singleton, but i read this from Jules:

[quote]In a plugin I don’t recommend using any kind of singleton - if you really need to share objects between your plugin instances, it’s much better to make them reference counted.[/quote] (link: http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=7491&hilit=singleton+plugin#p42302)

If I create a ReferenceCountedObject in the first VST instance…

  1. How can I avoid creating another one in the second one?
  2. How can I have a pointer in the second one that points to the object of the first one?

Thanks!

Sounds like all you need to do is to hold a static ReferenceCountedObjectPtr in yourClass and in getInstance() just
return this ReferenceCountedObjectPtr (create one if not exists).

The ReferenceCountedObject will take care to all the rest…

Your also better use

typedef ReferenceCountedObjectPtr<yourClass> Ptr;

I’d like to know how the second plugin instance finds the memory address of this singleton created by the first one.

Well, unless there’s something I’ve been missing, since both of the plug-ins are in the same process they share the same heap, and they both use the static Ptr which they also share in the code space to point to the instance.

You can use a static, but need be a bit smarter about it. When each plugin releases its pointer to the object, it should check whether its ref count has now gone down to 1 (i.e. no other plugin instances are using it, and only the static pointer is keeping it alive). In that case, it should zero the static pointer to make sure the object gets cleared up.

Right, I missed that :slight_smile:

Hmm okay so you are saying that when a DLL is loaded into a process there is only one instance, therefore only one set of variables with static storage duration.

remember this? viewtopic.php?f=8&t=8386

Thx guys, I think I could try it myself now on my PC.

A last question, does it work on a Mac? (I wont have a Mac until 2014 or so… but I need to create an architecture that actually works on both plataform.

Yep

Thanks you a lot!! :smiley:

No problem :slight_smile: