Hello,
I have a c++ coded DLL, which exports some functions in a flat C header file, so that the DLL can be used with several languages.
My problem is now to share some class static member (a singleton) between the application and the DLL. I can't export the member directly since my interface is not using classes.
I then tried change the implementation of this static member, and to turn it as a static variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Manager& Manager::GetInstance(int managerNumber)
{
static std::vector< std::tr1::shared_ptr<Manager> > Instances;
if(managerNumber>= ((int)Instances.size()))
{
for(int i = ((int)Instances.size()); i < managerNumber+1 ; i++)
{
//the shared_ptr call CustomDeleter() automatically once they go out of scope
std::tr1::shared_ptr<Manager> pManager( new Manager(),CustomDeleter<Manager>() );
pManager->_managerNumber=managerNumber;
Instances.push_back(pManager);
}
}
return *(Instances[managerNumber]);
}
In my application I created the same function, so the static vector is created with the same name.
Unfortunately, while running the debugger, it seems that 2 instances of this static vector are created, one "in" the DLL, and one "in" the application.
Is there any mechanism which would allow me to do that properly?
Thanks for any help.
Hum, after searching on the internet I got that a static variable declared in a function, even though it has the same name than another static variable declared in another function, will be different at run-time.
But then is there a way to do that properly? I could create a global scope variable in my DLL and export it so that the application can use it, but I find this solution very ugly :/
What I here call "interface" is the header file in my DLL where I am doing the exports.
This file is written in C++ which is compatible with C ( actually the whole header file is in a extern"C" tag). Thus the exported DLL is not compiler specific and can use C naming convention.
That's also why i don't want to export a class with this DLL, since there is no class in C.
I read that static local variables in class member functions will only have 1 instance of themselves, even throughout different translation units. And that's what I would like to use, to share this vector between my DLL and my application.
It's functioning good when I declare a static &Manager, but when I'm using this static vector it's not working any more.
Maybe I should declare every shared_pointer I'm adding to the vector as static?
"Static local data and strings in inline functions maintain the same identities between the DLL and client as they would in a single program (that is, an executable file without a DLL interface)."
Your question isn't very clear... but I can give some hints and tips:
static class variables and globals are absolutely evil. DO NOT export them from a DLL. You're just asking for issues with threading and various other things.
Instead, you can a) export functions that fetch the global variables and rather put them in either an anonymous namespace or give them the static attribute, don't directly expose the global variables, or b) pass the user a struct/class/pointer to some data that contains the variables needed for your library to work. In this case, void pointers or empty class pointers are not bad.
I think I understood the problem.
It was actually linked to a misunderstanding of the code that I'm modifying.
There is a class called Manager which is declared in the client application header file, and there is a class called Manager which is declared in the DLL itself.
But a Manager instance in the application can't be the same instance than a Manager instance in the DLL, right?
Then when I instantiate a Manager in the application I have to create an instance in the DLL, which will be a copy of the one in the application.
I will then modify the method Manager() in the application, to make it call Manager() in the DLL.