static-order-fiasco

c++ static initialization order fiasco

只谈情不闲聊 提交于 2021-01-27 11:21:20
问题 I'm currently learning C++, and I'm having some troubles. I've developped a program by using lots of #define , but I'd like to use static const instead (collision/type/scopes...). So, I now have something like: file1.hpp class A { public: static const std::string MY_CONST_VAR; }; file1.cpp const std::string A::MY_CONST_VAR = "some string"; file2.cpp static std::string arrayOfString[] = { A::MY_CONST_VAR, ... }; My code compiles with no warnings/errors (compiling with -W -Wall -Wextra -Werror

Deferred initialisation order in C++11

匆匆过客 提交于 2019-12-23 16:13:38
问题 Consider the following code, split across three compilation units: a.h : struct A { void Register(const char* s); const char* m_s[10]; int m_i = 0; }; A& GetA(); a.cpp : #include "a.h" #include <stdio.h> void A::Register(const char* s) { m_s[m_i++] = s; } A& GetA() { static A instance; return instance; } int main(int argc, char* argv[]) { A& a = GetA(); int n = a.m_i; for (int i = 0; i < n ; ++i) printf("%s\n", a.m_s[i]); return 0; } b.cpp : #include "a.h" struct B { B() { GetA().Register("b"

Does Java have the static order initialisation fiasco?

百般思念 提交于 2019-12-22 03:45:24
问题 A recent question here had the following code (well, similar to this) to implement a singleton without synchronisation. public class Singleton { private Singleton() {} private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } Now, I think understand what this is doing. Since the instance is static final , it's built long before any threads will call getInstance() so there's

c++ Schwarz counter with thread_local

左心房为你撑大大i 提交于 2019-12-11 08:03:02
问题 Can I use Schwarz counter (aka Nifty counter) idiom, with thread_local ? (Assuming I replace all static with thread_local ) I need this (helper for java jni threads): class ThisThread{ JNIEnv* jni_env{nullptr}; public: JNIEnv* getEnv(){ if (!jni_env){ // Attach thread java_vm->GetEnv((void**)&jni_env, JNI_VERSION); java_vm->AttachCurrentThread(&jni_env, NULL); } return jni_env; } ~ThisThread(){ if (!jni_env) return; // Deattach thread java_vm->DetachCurrentThread(); } }; static thread_local

Reproducing static initialization order fiasco in C++

大城市里の小女人 提交于 2019-12-11 05:01:18
问题 I read about the static initialization order fiasco in C++ relating to crash of the application. I think I understood it but still have few questions: 1) If I want to reproduce this problem, how can I do it (so that my program should crash)? I would like to write a test program to reproduce a crash. Can you please provide the source code if possible? 2) I read this C++ FAQ Lite article and it says that it has two static objects, x and y, in two different files and y calls x's method. How its

Undefined reference to static member of class template referenced from static instance

安稳与你 提交于 2019-12-11 04:15:24
问题 Please take a look at the following: #include <string> #include <unordered_map> template <int N> class Object; template <int N> class Thing; template <int N> class Factory { private: using FuncPtr = Object<N>*(*)(Thing<N>*); static std::unordered_map<std::string, FuncPtr> map; public: static void insertInMap (const std::string& tag, FuncPtr funcPtr) { map.emplace (tag, funcPtr); } }; template <int N> std::unordered_map<std::string, typename Factory<N>::FuncPtr> Factory<N>::map; // won't

How to force the linker to honor object file order?

我的梦境 提交于 2019-12-10 17:54:43
问题 I'm catching a Valgrind finding on an uninitialized read. I know exactly where its coming from - its an empty std::string declared in a cpp file with static storage class. The object file that has the std::string 's storage allocation is listed first in the static archive. # string of interest is located in a.o LIBOBJS := a.o b.o c.o ... x.o y.o z.o library.a: $(LIBOBJS) $(AR) $(ARFLAGS) $@ $(LIBOBJS) $(RANLIB) $@ In addition, I modified the link recipe to the following (I know it looks silly

Initializing qt resources embedded in static library

蹲街弑〆低调 提交于 2019-12-03 09:13:08
问题 I have next situation: I need to create widget in standalone static library, which then will be linked with final application (visual c++ 9.0, qt 4.5). This static widget library contains some resources (icons), and consist of a several .cpp files (each contains standalone widget). As far as I know, i must initialize qt resource system, if i use them (resources) in static library, with call to "Q_INIT_RESOURCE( resource_file_name )". I solved this with next code (in every .cpp file in static

Initializing qt resources embedded in static library

安稳与你 提交于 2019-12-02 23:20:59
I have next situation: I need to create widget in standalone static library, which then will be linked with final application (visual c++ 9.0, qt 4.5). This static widget library contains some resources (icons), and consist of a several .cpp files (each contains standalone widget). As far as I know, i must initialize qt resource system, if i use them (resources) in static library, with call to "Q_INIT_RESOURCE( resource_file_name )". I solved this with next code (in every .cpp file in static library): #include <QAbstractButton> namespace { struct StaticLibInitializer { StaticLibInitializer() {

Prevent static initialization order “fiasco”, C++

爷,独闯天下 提交于 2019-11-29 13:46:58
Once I was reading an awesome C++ FAQ (It is really good!!) and read the topic about how to prevent the static initialization order "fiasco". So the author advises to wrap the static variables into functions, thus to prevent the "fiasco" by maintaining the creation order of variables. But this seems to me a rude workaround. So my question is, is there any modern, more pattern oriented way to prevent this "fiasco" but to wrap the "static stuff" into functions??? The modern, more pattern-oriented way is not to use globals in the first place . There's no other way around it. It wouldn't be much