1

I saw several questions about static local variable and static member of a class. From one comment in this and probably the most clear one link

C++ Primer says:

Each local static variable is initialized before the first time execution passes through the object's definition. Local statics are not destroyed when a function ends; they are destroyed when program terminates.

But I found that the above description "when program terminates" is ambiguous. Does it mean the application exit? or if it is a plugin, does it mean the plugin is unloaded?

I have noticed that local static variable is not destroyed in my plugin is unloaded when it is compiled in release mode on Linux before. But when I changed it to a class member, it is destroyed properly when the plugin is unloaded. The class/local static variable is used in the plugin only.

Can anybody give some explanation?

Community
  • 1
  • 1
  • possible duplicate of [Does C++ call destructors for global and class static variables?](http://stackoverflow.com/questions/2204608/does-c-call-destructors-for-global-and-class-static-variables) – IdeaHat Jan 19 '15 at 18:04
  • I don't think it answered the question here I'm asking. Here I'm asking for code in a plugin. The start and end of program for a plugin, my understanding is started when you load the plugin into your app, the end of the program is when you unload the plugin from your program (NOT the end of the application itself). If that's the case, I think a local static variables should be destroyed when the plugin is unloaded, but somehow I did notice different behavior. I'm not sure if that's a bug in the compiler/system, or it's the ambiguity of the sentence I quota above or even in the standard. – user1073719 Jan 19 '15 at 19:52

1 Answers1

0

The static storage area is managed by the implementation, not by the plugin.

Remember that if it were an ordinary function and not a plugin, the function could be called multiple times and you'd expect to find the data in the static object the same as the previous time it was called.

The proper behaviour is that if you could unload and reload your plugin, the local static variables would preserve their values, so they can't be destroyed until the implementation exits.

tipaye
  • 454
  • 3
  • 6