Creating multiple instances of global statistics in C ++?

One of the libraries that we use for our product uses singleton access to access it. I am sure that it is implemented as a static instance (it is not open source). This works well for a single document application, but more than one document can be loaded in our application. I assume access to the instance is written something like this:

Instance* getInstance() { static Instance* inst = new Instance(); return inst; } 

In such situations, is there some way to create more than one instance? The only thing I can think of is to have more than process and use some type of IPC to tie it all together. I can't think of anything less hacked.

I asked the provider to implement some type of session token, so I can have several parallel instances, but they are large, and we are small.

Corey

Edit:

  • the machine is a windows machine
  • global statics is basically a big factory. I need some kind of session token, so I can easily say โ€œfree all resources from this sessionโ€ (there is no way to re-initialize the global statics that I know about)

Instead of trying to use some dodgy fraud to get what I want, I'm going to wrap it all in my class and add a session key to each recipient. Inside, I will keep track of what has been allocated, adding my own release method to return the resources. This is suboptimal for many reasons, but I can't come up with a better idea.

Thanks everyone for the great feedback.

+4
source share
5 answers

The only thing I can think of is to subclass it, if you're lucky, if you have one such class defined:

 class Document { public: static Document* getInstance() { static Document inst; return &inst; } virtual ~Document(); protected: Document(); private: struct Impl; Impl *pImpl; }; 

If you can subclass it, and the subclass will have access to the constructor, you can create an instance subclass, for example:

 class MyDocument: public Document { public: MyDocument(): Document() { } }; 

This may not be entirely safe, as the developer may have made some unpleasant assumptions. But is this an idea or some kind of approach that may have some chance of working. With any luck, the supplier may be amenable to this option if you mention this ... good luck.

+1
source

Even if you could solve this particular problem when everything happened in-proc, I would be worried that this singleton problem is just the tip of the iceberg. Obviously, the library was not designed for your scenario.

Isolating each DLL download into its own process sounds right and non-hacks to me, although of course it can be expensive for you.

+5
source

Unfortunately, I do not see a flaw in your reasoning. The seller has made a decision, and you are bound by him. He made the decision of one instance for each process, so if you need several instances, you must have several processes with everything that entails.

Of course, if you assume that his decision to limit is arbitrary and that there is no good reason for this, you can try to crack it. The path to the beginning of this path is to perform a maladaptation / build in the debugger. If you can confirm that its factory instance works exactly the same as you did, you can no doubt hack an alternative that allows you to create multiple instances.

But, of course, the huge risk with this approach is that each line of code in the vendorโ€™s code base, which relies on its decision to have one copy, is a temporary bomb, ready to explode in your face. This code is invisible to you. Are you ready to bet that there are no such lines? I know that Clint Eastwood would say in such a situation; โ€œYou feel happy punk, okay?โ€ :-)

+4
source

There are no elegant ways to have multiple instances of a singleton object in the same program space - but this is on purpose. In general, you only use a singleton when it is undesirable to have multiple instances. If a vendor has implemented their product using a singleton, there may be good reasons for choosing.

Perhaps if you describe your problem in more detail, other approaches are possible. It is hard to say based on the information provided. What does a singleton object do? Why do you need multiple instances?

+2
source

With the exception of the interprocess material that you suggested, the best hack way I can come up with is to copy the dll to a new file and manually load the new dll and import all the functions that you use for each instance you create.

Hopefully this means that static variables will not conflict between different instances, as they are technically not in the same DLL. However, there are many bad things in this solution, such as all the code in the dll cloned for each version, and you cannot use the import library, but you must load the DLL and import all the functions manually.

+1
source

All Articles