Jump to content
  • Advertisement
Sign in to follow this  
Pooya65

dynamic loading dll and singlton implemention problem

This topic is 3344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i'm load dlls dynamically in my application (using LoadLibraryA functions) and then i dll files i use singleton class. problem is in singleton class created again in dll functions!!!. how i can prevent this?

Share this post


Link to post
Share on other sites
Advertisement
Hello ,

I think as far as i understood,a class is supposed to be a singleton but the dll functions spawn multiple instances.
If you don't own the Dll code there is not much you can do.


Raxvan

Share this post


Link to post
Share on other sites
If you do something like this in your header file..:


class Singleton {
Singleton() {}

public:
static Singleton& GetInstance() {
static Singleton inst;
return inst;
}

void Foo() {
// ...
}

// ...
};




..ie., the entire definition and implementation of the class is in the header file alone, then there will a copy of Singleton::GetInstance's inst in the image of every project that includes the header file (and attempts to use Singleton::GetInstance).

You need to make sure there is only one instance of the singleton class, and that it is in your application's image.

Then, you need to tell the DLL where this instance is by passing it a reference to it somehow - be this a pointer in a call to an initialization function, or by having a function it can callback to get the instance. It's really no different to passing a reference to any other non-singleton object to the DLL - there is no difference from the DLL's perspective.

E.g., Application header file:

class Singleton {
Singleton();

public:
static Singleton& GetInstance();

virtual void Foo(); // function is virtual so the DLL can find the method
// in the vtable from the passed Singleton reference,
// since the DLL is loaded at runtime

// ...
};




Application source file:

Singleton& Singleton::GetInstance() {
static Singleton inst;
return inst;
}

void Singleton::Foo() {
// ...
}




In a DLL header file, which you can then include to get access to the instance of Singleton:

extern Singleton *instance;




In a DLL source file:

Singleton *instance;

extern "C" __declspec(dllexport) void SetSingletonInstance(Singleton *inst) {
instance = inst;
}




Where you load the DLLs in the application:

HMODULE hDLL = LoadLibrary("mydll.dll");
void (*setInst)(Singleton *) = GetProcAddress(hDLL, "SetSingletonInstance");
setInst(&Singleton::GetInstance());


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!