[libcamera-devel] [PATCH 01/38] Documentation: coding-style: Document global variable guidelines

Paul Elder paul.elder at ideasonboard.com
Tue Sep 22 15:35:00 CEST 2020


Document the issue related to global variable dependencies and how to
avoid them.

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>

---
New in v2
---
 Documentation/coding-style.rst | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/Documentation/coding-style.rst b/Documentation/coding-style.rst
index 8af06d6a..967506db 100644
--- a/Documentation/coding-style.rst
+++ b/Documentation/coding-style.rst
@@ -187,6 +187,21 @@ 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 initialization and destructions of global variables cannot be
+reasonably controlled. This can cause problems (segfaults) when global
+variables depend on each other, or when non-globals depend on globals.
+For example, if the declaration of a global variable calls a constructor,
+which calls into another global variable that has yet to be initialized, that
+is likely to segfault.
+
+The best solution is to avoid global variables when possible. If required,
+such as for implementing factories with auto-registration, avoid dependencies
+by running the minimum amount of code in the constructor and destructor,
+and move code that contains dependencies to a later point of time.
+
 C Compatibility Headers
 ~~~~~~~~~~~~~~~~~~~~~~~
 
-- 
2.27.0



More information about the libcamera-devel mailing list