Sign in to follow this  
Alundra

.CRT Warnings : Global static variables

Recommended Posts

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

Edited by Alundra

Share this post


Link to post
Share on other sites

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) Edited by SmkViper

Share this post


Link to post
Share on other sites

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 ?

Edited by Alundra

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

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...)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this