Bug: gCASEAuthDelegate was constructed twice#560
Bug: gCASEAuthDelegate was constructed twice#560kghost wants to merge 1 commit intoopenweave:masterfrom
Conversation
Using a global variable and placement new will call the constructor twice. Use C++ static singleton to allocate the object instead. Change-Id: Ib7e3c9a70d8d40d08b115e4f57d4ed3a8378c20f
|
|
||
| WEAVE_ERROR InitCASEAuthDelegate() | ||
| { | ||
| new (&gCASEAuthDelegate) CASEAuthDelegate(); |
There was a problem hiding this comment.
The new of placement new was intentional, to support systems that do not run global constructors.
Is there a particular problem with this you're trying to work around?
There was a problem hiding this comment.
There is no obviously problem yet, but currently it may not be the best solution, with following reasons.
- There are more platforms which support global constructors, the the constructor will be called twice.
- With the PR changes, the static variable will still be allocated in the global segment, and the constructor will be called at the first time
InitCASEAuthDelegateis called, it solves the problem on all platforms with or without global constructors being called. - The singleton design pattern is widely used after C++11 has introduced thread-safe static variable construction.
After all, I think that using C++11 singleton pattern is better than global variable + placement new.
BTW, I'm interested in the fact that there are platforms which doesn't run global constructors.
I know that for dynamic linked program or library, global constructors are compiled and linked into .init_array section of the ELF file. and the .init_array section contains lots of library initializing code, not only used for C++, but also C programs, it will be executed by the loader (ld-linux).
For static linked program, I think it will be directly linked and called after program start. I don't think it is platform related feature, it is decided by the toolchain.
Using a global variable and placement new will call the constructor
twice. Use C++ static singleton to allocate the object instead.
Change-Id: Ib7e3c9a70d8d40d08b115e4f57d4ed3a8378c20f