   dynamic lib loading
  I have a problem with instance variables accross dnynamic libs (.dll or .so files).If I store a pointer of the class like this:   class MainClass{public:    static MainClass* singleton;    MainClass() { singleton = this; }};  and then, after that, if I load a dynamic library and try to access that 'singleton' variable...I get a NULL value:   // This is located in another file as a shared libraryclass DynLib{   DynLib() { MainClass::singleton->doSomething() }};  That is, MainClass::singleton = 0, giving a null exception...Does anyone know what's wrong?Thanks for your help.[edited by - _sid_ on February 10, 2004 12:50:24 PM][edited by - _sid_ on February 10, 2004 12:51:03 PM]
  It sounds like separate copies of the storage for the singleton variable are being declared in the executable and the dynamic library. One method to fix this is to make sure all the singleton scaffolding goes into a dynamic library so that the executable and the dynamic library link against the same copy of the storage for the variable. ;  And would someone kindly explaining how that can be done exactly? ;  Given the fact that you obviously know how to export a class from a dynamic library, I''m not sure what you would be having a question about here. ;  Where is actually the instance of MainClass::singelton stored? ;  SiCrane: Duh! Obviously. I know that you have to export. And besides, on gcc, there's no such thing as exporting (i.e. no export thing before class name) That was not my question. My question was how to ensure that only one copy of a variable is maintained across shared libraries.VolkerG: in MainClass, that's where it's supposed to (theortically) be stored in.   [edited by - _sid_ on February 10, 2004 3:48:53 PM];  Well, _sid_, but you should have a line likeMainClass *MainClass::singelton = 0Where is it located. ;  Quick question - have you actually constructed an instance of MainClass anywhere? Jans. ;quote:Original post by _sid_  My question was how to ensure that only one copy of a variable is maintained across shared libraries.Programmer discipline. If you don''t want storage for a variable to be allocated in a given module, don''t include the object file that contains the variable''s storage when you link the module. 