-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Description
Bugzilla Link | 22281 |
Version | 3.5 |
OS | Linux |
Reporter | LLVM Bugzilla Contributor |
CC | @majnemer |
Extended Description
The following code produces different symbol types in the object file when compiled with clang and gcc. I do not have any error or crashes.
//singleton.cpp
class Singleton
{
public:
static Singleton* getInstance()
{
static Singleton* mInstance=0;
if (!mInstance)
{
mInstance=new Singleton();
}
return mInstance;
}
};
void foo()
{
Singleton* myBar = Singleton::getInstance();
}
//
Compile command:
% clang -o clang.o -c singleton.cpp && gcc -o gcc.o -c singleton.cpp
nm then shows the following for both object files:
% nm clang.o && echo && nm gcc.o
0000000000000000 T _Z3foov
0000000000000000 W _ZN9Singleton11getInstanceEv
U _Znwm
0000000000000000 V _ZZN9Singleton11getInstanceEvE9mInstance
0000000000000000 T _Z3foov
0000000000000000 W _ZN9Singleton11getInstanceEv
U _Znwm
0000000000000000 u _ZZN9Singleton11getInstanceEvE9mInstance
As can be seen _ZZN9Singleton11getInstanceEvE9mInstance is created as "V" = weak object by clang and "u" = unique global symbol by gcc.
I think this difference is the underlying problem I experience in a larger code base that uses the singleton pattern. The created executables and dynamic libraries do not work correctly because the singletons are not unique in the process. I observed multiple constructor calls while debugging. The singletons are created in a library and are used across library boarders.
Observed on Fedora 21 with clang 3.5.0-6.fc21 and gcc 4.9.2-1.fc21.
I also observed this behaviour on Fedora 20 but do not recall the actual compiler versions.