diff options
author | Paul Elder <paul.elder@ideasonboard.com> | 2020-09-21 14:00:19 +0900 |
---|---|---|
committer | Paul Elder <paul.elder@ideasonboard.com> | 2020-10-07 19:17:31 +0900 |
commit | 7e59bccb35015dea1a3ce6c50efb0cc23157060f (patch) | |
tree | 473db885672682b00531d8cc1fbfe3e803853342 | |
parent | 6193e9c25277c8fd16bb0475c640cc29104115de (diff) |
Documentation: coding-style: Document global variable guidelines
Document the issue related to global variable dependencies and how to
avoid them.
Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r-- | Documentation/coding-style.rst | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Documentation/coding-style.rst b/Documentation/coding-style.rst index 8af06d6a..71d5c0b2 100644 --- a/Documentation/coding-style.rst +++ b/Documentation/coding-style.rst @@ -187,6 +187,25 @@ These rules match the `object ownership rules from the Chromium C++ Style Guide` long term borrowing isn't marked through language constructs, it shall be documented explicitly in details in the API. +Global Variables +~~~~~~~~~~~~~~~~ + +The order of initializations and destructions of global variables cannot be +reasonably controlled. This can cause problems (including segfaults) when global +variables depend on each other, directly or indirectly. For example, if the +declaration of a global variable calls a constructor which uses another global +variable that hasn't been initialized yet, incorrect behavior is likely. +Similar issues may occur when the library is unloaded and global variables are +destroyed. + +Global variables that are statically initialized and have trivial destructors +(such as an integer constant) do not cause any issue. Other global variables +shall be avoided when possible, but are allowed when required (for instance to +implement factories with auto-registration). They shall not depend on any other +global variable, should run a minimal amount of code in the constructor and +destructor, and code that contains dependencies should be moved to a later +point in time. + C Compatibility Headers ~~~~~~~~~~~~~~~~~~~~~~~ |