Linking .lib files with static variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I'm having some trouble with a .lib file in a project I'm working on.

The file contains a class which implements the singleton pattern.

To implement it I've declared a static pointer to the class called instance
outside of the class. The code looks something like this(the project is very
large and I haven't been able to build a cut-down version of the problem
because I'm not familiar with all the settings):

static Singleton* instance = 0;

class Singleton
{
public:
static Singleton* getInstance()
{
if(instance == 0)
{
instance = new Singleton();
}
return instance;
}

.....
}

So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.

Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....

I've tried changing the way my singleton is implemented but have not been
able to implement it using any other method so far. I don't even really think
that that is the problem. I'm starting to think that no matter how I
implement the class it's going to be returning two different instances of the
class.

Has anyone encountered anything like this before? I have a feeling that it
is some sort of linker or VC++ specific problem that I'm having.

Cheers
Johnny
 
....
So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.

Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....

Johnny,

You need to arrange to only have a single instance. As soon as you
have the same data in 2 DLLs you're going to get in the situation you
are in.

I'd change things so that I only had that code and data in a single
DLL and I'd export a getInstance() function from it for other
modules. If you want to export the class, have a look at "Using
dllimport and dllexport in C++ Classes" in MSDN.

Dave
 
Thanks for the reply.

So now for implementing your solution... :)

I'm actually working with a mostly COM based application (well I think,
COM/MFC etc... etc... are all MS crap that I have never really learnt and
prefer to use it as little as possible) written for VS6 which has been
re-compiled with VS .NET 2003 and most of the projects are compiled with
/clr.

My singleton class is an unmanaged class which contains a managed object
referenced by gcroot<>. This class was a replacement for another class in the
lib file referenced by the two Dll's. As you know this screwed my singleton
implementation. So now I've decided to remove the project which built the lib
file (It's no longer necessary) and replace it with a .NET Class Library
containing my singleton class.

So I've got my class library building correctly but I can't seem to use it
in the two Dll's which require the class.

I've included the #using "Singleton.dll" in the necessary places but the
compiler does not seem to be able to recognise my Singleton class (I get lots
of C2065's).

There appears to be a few possibilities here:
1. I'm not including the Singleton Dll in the unmanaged code in the correct
way.
2. I'm not building the right type of Dll. I need to go for the old pre-.NET
Dll. (Which I don't know how to build, so if you can get me started that'd be
nice)
3. Do I need to link the Dll the old-fashioned way using the header file and
lib file? (My .NET class library doesn't generate a lib file so if this is a
solution you'll have to tell me how to do it).
4. I've got no idea how to do interop and need a good beating.

Both the Dll's are compiled with /clr and previously had no problem using my
singleton class (except that it wasn't really a singleton, which screws
things up greatly).

So any idea's on what I'm doing wrong?

Thanks for your help.
Johnny
 
I'm afraid that I'm no help to you when it comes to managed code -
I've shied away from it and stuck with the devil I (mostly) know until
such time as I really need to make a move.

The only thing that comes to mind is that the complexity of your
existing projects may be confusing the issue, I'd be inclined to
create new projects just to prototype the arrangements between them
and be sure it'd work - it might be easier to work with that way?

Dave
 
Back
Top