You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The assumption of SessionLocal is that all global objects will be automatically and fully initialized (allocated and constructed) at the beginning of the whole program (before main() is invoked). But this is not practical.
As stated in C++ standard:
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization.
And also
It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.
In short, constructors of global objects are dynamic initialization and dynamic initialization is not guaranteed to be done before main(), but is guaranteed before the first call.
One solution to this is, for each thing that needs to be initialized, initialize it at the first time it is used. Meanwhile, if it needs to be finalized, register a finalizer when it's initialized. The finalizer is called at the end of the program.
The text was updated successfully, but these errors were encountered:
The assumption of
SessionLocal
is that all global objects will be automatically and fully initialized (allocated and constructed) at the beginning of the whole program (beforemain()
is invoked). But this is not practical.As stated in C++ standard:
And also
(From StackOverflow)
In short, constructors of global objects are
dynamic initialization
anddynamic initialization
is not guaranteed to be done beforemain()
, but is guaranteed before the first call.One solution to this is, for each thing that needs to be initialized, initialize it at the first time it is used. Meanwhile, if it needs to be finalized, register a finalizer when it's initialized. The finalizer is called at the end of the program.
The text was updated successfully, but these errors were encountered: