Jump to content
  • Advertisement
Sign in to follow this  
Alundra

.CRT Warnings : Global static variables

This topic is 1315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!