From 01e387acb0cc345fc9992498898efd167bf07b71 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 23 May 2021 02:51:32 +0300 Subject: libcamera: log: Destroy LogCategory instances in a controlled way The LogCategory instances are constructed on first use as static variables in accessor functions, following the Meyers singleton pattern. As a result, their destruction order is not guaranteed. This can cause issues as the global Logger object, constructed in a similar fashion, is accessed from the LogCategory destructor and may be destroyed first. To fix this, keep the same singleton pattern, but allocate the LogCategory instances dynamically. As they get registered with the global Logger instance, we can destroy them in the Logger destructor. This only avoids destruction order issues between LogCategory and Logger, and doesn't address yet the fact that LOG() calls from destructors of global objects may access an already destroyed Logger. Signed-off-by: Laurent Pinchart Reviewed-by: Hirokazu Honda Reviewed-by: Kieran Bingham --- include/libcamera/internal/log.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/libcamera/internal/log.h b/include/libcamera/internal/log.h index b66bf55b..b8efb161 100644 --- a/include/libcamera/internal/log.h +++ b/include/libcamera/internal/log.h @@ -29,7 +29,6 @@ class LogCategory { public: explicit LogCategory(const char *name); - ~LogCategory(); const char *name() const { return name_; } LogSeverity severity() const { return severity_; } @@ -48,8 +47,9 @@ extern const LogCategory &_LOG_CATEGORY(name)(); #define LOG_DEFINE_CATEGORY(name) \ const LogCategory &_LOG_CATEGORY(name)() \ { \ - static LogCategory category(#name); \ - return category; \ + /* The instance will be deleted by the Logger destructor. */ \ + static LogCategory *category = new LogCategory(#name); \ + return *category; \ } class LogMessage -- cgit v1.2.1