开发者

How do I reinitialize a singleton that I don't have full control over in the middle of an app?

开发者 https://www.devze.com 2023-04-03 07:32 出处:网络
I have a compiled external library that I\'m using in my (Objective-C++) code. This library has a singleton class to store some information throughout the duration of the application being us开发者_如

I have a compiled external library that I'm using in my (Objective-C++) code. This library has a singleton class to store some information throughout the duration of the application being us开发者_如何学运维ed. I really need a way to be able to remove the loaded information and reload it in the middle of the application. Or to reload the application so that this singleton is reinitialised.

In the header files there is no way to reset the class information.

The way I get an instance of it is:

Singleton::getInstance();

In the header file this is shown as:

static Singleton& getInstance();

I would like a way to be able to delete this instance and reinitialise another instance.

Is there any way to do this? This singleton is only being used on one (the main) thread.

Extra information

When the application first loads I have to call:

Singleton::getInstance().load();

so that it loads information from the Documents directory of the app and create an internal structure. I replace those files in the Documents directory in the middle of the application running and I want it to reinitialise. If I call the above function again I get an error because the person who wrote this library doesn't allow the load function to be called twice.

I tried some stuff like:

delete &Singleton::getInstance();

but I don't know how I should reinitialise it afterwards. As calling

Singleton::getInstance();

crashes the app.


Unless the API is providing you a way to do it, it's probably impossible.

But I would have try to rerun the "load" function after I call the delete...


Try instantiating the Singleton from within a dinamic link library, instead of from within your own application. If you need to reinstantiate, unload the dll (or .so depending on the OS you are running on) module and reload it.

Not sure if this works though, but I guess it is worth a try.

Later edit: just noticed the iOS tag. Don't know if iOS has dynamic loadable modules, so my answer may not apply.

0

精彩评论

暂无评论...
验证码 换一张
取 消