This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: duplicate singleton on Windows-platform


On Thu, 7 Feb 2008, Chris Bielow wrote:

> I came a little closer to solving the problem...
>
> The problem was, that I there existed *two* object files (one in the
> shared lib, one in the executable) of the Factory at runtime.
>
> To solve this I only declared the instance() function of Factory.h when
> including it in an executable, but defined it within the Library. This
> causes the linker to use the object from the lib.

I'm surprised that worked at all.
You are still violating the rule that says you should
only define a variable (e.g. a static data member) once.

> ----------- Factory.h ----------
> ...
> /// singleton access to Factory
> static Factory* instance()
> #ifdef OPENMS_WITHIN_LIB
> {
>    if (!instance_ptr_)
>    {
>      instance_ptr_ = new Factory();
>      FactoryProduct::registerChildren();
>    }
>    return instance_ptr_;
> }
> #else
> ;
> #endif
> ----------- END Factory.h -----------
>
> Now it works on Windows as well.
>
> BUT: new problem.
> If the executable wants to instanciate a new kind of Factory (other
> Template parameter), the compilation fails, because the instance()
> method is only declared, but not defined.

Multiple identical definitions of the same instance()
are ok if they are all within a class definition.

> How can I convince the linker to use instances of the Factory from the
> Lib and creating new instances only if they are not present in the Lib.
>
> One option would be to instanciate every Factory within the lib that
> might be used by an executable.. but thats rather nasty.

I'm not sure how much progress has been made on
template-handling since I last paid attention.

Keep instance() in all uses of Factory.h .
Keep the *declaration* of instance_ptr in all uses of Factory.h .
Don't *define* any instance_ptr's in the library.
Define them outside the library, possibly with main or in their own files.
That should work even if no progress has been made.
It might also make the Factory library rather small.

Whenever you use a type not built into the library,
"linking" will require running the compiler again.
The result will not be shared.
This is true whether you use shared libraries or not.
It does rather defeat a purpose of shared libraries.

There is another issue with shared libaries and templates.
Compiling in Factory<Fred> could cause trouble for those
wanting to use Factory<Fred> for a different Fred.


BTW do you need a singleton?
Is there a reason you can't just make all members static?

-- 
Michael   hennebry@web.cs.ndsu.NoDak.edu
"Those parts of the system that you can hit with a hammer (not advised)
are called Hardware;  those program instructions that you can only
curse at are called Software."



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]