-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GetModuleOrder() result has unspecified order of destruction #53
Comments
There is a provision for this in the Google style guide.
Also since static objects are not allowed by the style guide, this should not be an issue. The only objects which CAN call this function during destruction would be function local objects with static lifetimes. The destruction of these objects is well defined by the standard. If one of the function local objects were to access this method in the destructor then you can define the order of creation(and therefore destruction) by calling the GetModuleOrder before the other object is created.
This approach is also thread safe in the C++11 standard. Reference: http://stackoverflow.com/a/8102145/523374 |
Umar, I'm not sure I agree with your guide interpretation. The exception you quote does not reverse the ban on function static variables (additionally reinforced by the second paragraph). Instead, it's an exception to the sentence immediately preceding it:
Here's how the dangling-reference danger could play out:
Although |
The order in which
This code will cause a problem because GetModuleOrder is called after the static Foo object is initialized. And since order of destruction is the opposite of the other of construction the vector module is called In order to fix this type of issue all you would need to do is to call
Anyway, the only reason the Google style guide gives is that the order is not defined for static objects. Function local static objects have a defined order of construction and destruction so I don't think that rule should apply to those objects. |
Thanks for correcting my example; I agree that it didn't illustrate the danger well because of the deterministic destruction order. Of course, it's still too easy to accidentally reorder I'm not comfortable with ignoring parts of the style guide (however much I may find them objectionable) without a broad agreement and a written-down list of exceptions first. |
The function is only used to support ValidationState_t::isOpcodeInCurrentLayoutStage |
Currently,
GetModuleOrder()
returns a reference to a staticvector
object. This is a violation of the style guide because other static objects may accidentally dereference this in their destructors, without any guarantee that theGetModuleOrder
vector isn't already destroyed. Instead of creating a staticvector
, the method should either use a C array or a static pointer to a dynamically allocatedvector
.References:
https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
https://isocpp.org/wiki/faq/ctors#construct-on-first-use-v2
The text was updated successfully, but these errors were encountered: