.CRT Warnings : Global static variables

Started by
4 comments, last by SmkViper 9 years ago

Hi all,

I have a list of variable in the anonymous namespace in one .cpp and a class with only static function to get them in the .h.

Two static function inside the same class : Init() and Shutdown() used to call class init function when needed on these variables and order of destroy.

All works fine in static lib but when I compile in shared lib I got lot of warnings like that :

warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

And a crash on the call of the Init() function.

How solve this problem ?

Thanks for the help

Advertisement

Hi all,
I have a list of variable in the anonymous namespace in one .cpp and a class with only static function to get them in the .h.
Two static function inside the same class : Init() and Shutdown() used to call class init function when needed on these variables and order of destroy.
All works fine in static lib but when I compile in shared lib I got lot of warnings like that :





warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

And a crash on the call of the Init() function.
How solve this problem ?
Thanks for the help


Shared libraries cannot have static data because there is no way to actually set up the static data when the library is used, or a way to shut it down when the library is no longer in use. (This also can cause a problem because two programs using the same shared library could then access data cross-process, when isn't usually allowed)

This is yet another reason to not use globals (tm). At the very least you should store them in some sort of allocated memory structure that is then passed to the functions that need access to them.

Disregard - I didn't read the error documentation like a noob. (Global state is still bad though)

The only way to avoid this problem and remove warnings is to have an extern pointer of the class/struct exported and have it initialized by the application who uses the lib ?

Hi all,
I have a list of variable in the anonymous namespace in one .cpp and a class with only static function to get them in the .h.
Two static function inside the same class : Init() and Shutdown() used to call class init function when needed on these variables and order of destroy.
All works fine in static lib but when I compile in shared lib I got lot of warnings like that :


warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

And a crash on the call of the Init() function.
How solve this problem ?
Thanks for the help


Shared libraries cannot have static data because there is no way to actually set up the static data when the library is used, or a way to shut it down when the library is no longer in use. (This also can cause a problem because two programs using the same shared library could then access data cross-process, when isn't usually allowed)

This is yet another reason to not use globals (tm). At the very least you should store them in some sort of allocated memory structure that is then passed to the functions that need access to them.

That's not true. When an executable loads a shared library it gets its own copy of all global variables.

MSDN has documentation on this error: https://msdn.microsoft.com/en-us/library/708by912(v=vs.71).aspx

I already saw the MSDN documentation but I don't have the "entry" thing already, it's more a problem of init apparently.

That's not true. When an executable loads a shared library it gets its own copy of all global variables.

MSDN has documentation on this error: https://msdn.microsoft.com/en-us/library/708by912(v=vs.71).aspx


That'll teach me not to look up the error documentation before opening my big mouth. Sorry about that.

(Plus I've never made a DLL that didn't link into the CRT...)

This topic is closed to new replies.

Advertisement