[英]How to delay destruction of static object (logger) in C++?
I got a class which holds the application's logger as a unique_ptr.我有一个类,它将应用程序的记录器保存为 unique_ptr。 The logger can be set through a static function.可以通过静态函数设置记录器。 Also, one can obviously log messages.此外,显然可以记录消息。 I left out any thread synchronization (mutexes) to make things easier.我省略了任何线程同步(互斥锁)以使事情变得更容易。
class LoggerWrapper {
public:
static void SetLogger(std::unique_ptr<logger::ILogger> new_logger);
static void Log(const std::string& message);
private:
static std::unique_ptr<logger::ILogger> logger_;
};
void LoggerWrapper::Log(const std::string& message) {
if (!logger_) {
// cannot log
} else {
logger_->OnLogEvent(message);
}
}
void LoggerWrapper::SetLogger(std::unique_ptr<logger::ILogger> new_logger) {
logger_ = std::move(new_logger);
}
My problem is: The unique_ptr gets destructed before some of the other classes inside the application.我的问题是: unique_ptr 在应用程序中的其他一些类之前被破坏。 Eg is the DTOR of Class Foo
wants to log something, the unique_ptr might have already been destroyed (which is the case at the moment).例如, Class Foo
的 DTOR 想要记录一些东西,unique_ptr 可能已经被销毁(目前就是这种情况)。 This causes the ILogger
implementation to be destroyed, resulting in no log output being possible.这会导致ILogger
实现被破坏,导致无法输出日志。
Does anyone have an idea on how to easily fix this?有没有人知道如何轻松解决这个问题? I somehow need to "delay" the destruction of the static unique_ptr.我不知何故需要“延迟”静态unique_ptr的破坏。 I also tried changing it to a shared_ptr, but that only caused SIGABRTs with "pure virtual method called" errors.我还尝试将其更改为 shared_ptr,但这只会导致 SIGABRT 出现“调用纯虚拟方法”错误。
Thanks in advance!提前致谢!
EDIT: Created a minimal working example which contradicts my experience.编辑:创建了一个与我的经验相矛盾的最小工作示例。 In this case, the static logger outlives the Foo
class.在这种情况下,静态记录器的寿命比Foo
类长。
EDIT2: My application uses exit
. EDIT2:我的应用程序使用exit
。 That seems to change the order of destruction.这似乎改变了破坏的顺序。
EDIT3: exit
does not destroy local objects. EDIT3: exit
不破坏本地对象。
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class ILogger {
public:
ILogger() {
std::cout << "ILogger CTOR" << std::endl;
}
~ILogger() {
std::cout << "ILogger DTOR" << std::endl;
}
virtual void OnLogEvent(const std::string& log_message) {
std::cout << "OnLogEvent: " << log_message << std::endl;
}
};
class LoggerWrapper {
public:
static void SetLogger(std::unique_ptr<ILogger> new_logger) {
logger_ = std::move(new_logger);
}
static void Log(const std::string& message) {
if (!logger_) {
// cannot log
} else {
logger_->OnLogEvent(message);
}
};
private:
static std::unique_ptr<ILogger> logger_;
};
class Foo {
public:
Foo(const std::string& name) : name_{name} {
LoggerWrapper::Log(name_ + ": CTOR");
}
~Foo() {
LoggerWrapper::Log(name_ + ": DTOR");
}
private:
std::string name_;
};
// declaring logger_ first causes it to be deleted AFTER foo
std::unique_ptr<ILogger> LoggerWrapper::logger_;
std::unique_ptr<Foo> foo;
int main()
{
LoggerWrapper::SetLogger(std::make_unique<ILogger>());
foo = std::make_unique<Foo>("Global FOO");
// local variables do NOT get destroyed when calling exit!
auto foo_local = Foo("Local FOO");
exit(1);
}
This is trivial.这是微不足道的。
First you don't use global static objects ( you should not be using global state like that).首先,您不使用全局静态对象(您不应该使用这样的全局状态)。 You use function static objects so you can control the order of creation/destruction.您使用函数静态对象,以便您可以控制创建/销毁的顺序。
So change this:所以改变这个:
std::unique_ptr<ILogger> LoggerWrapper::logger_;
std::unique_ptr<Foo> foo;
Into:进入:
class GlobalLogger
{
public:
ILogger& getLogger() {
static ILogger logger; // if you must use unique_ptr you can do that here
return logger; // But much simpler to use a normal object.
}
};
class GlobalFoo
{
public:
Foo& getFoo() {
// If there is a chance that foo is going to
// use global logger in its destructor
// then it should simply call `GlobalLogger::getLogger()`
// in the constructor of Foo. You then
// guarantee the order of creation and thus destruction.
// Alternatively, you can call it here in thus
// function just before the declaration of foo.
static Foo foo;
return foo;
}
};
// Where you were using `logger_` use `GlobalLogger::getLogger()`
// Where you were using `foo` use `GlobalFoo::getFoo()`
If we use your original code as the starting point we can do this:如果我们使用您的原始代码作为起点,我们可以这样做:
#include <iostream>
#include <memory>
#include <string>
// Please don't do this.
// This is the number one worst practice.
// https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
using namespace std;
class ILogger {
public:
ILogger() {
std::cout << "ILogger CTOR" << std::endl;
}
~ILogger() {
std::cout << "ILogger DTOR" << std::endl;
}
virtual void OnLogEvent(const std::string& log_message) {
std::cout << "OnLogEvent: " << log_message << std::endl;
}
};
class LoggerWrapper
{
// Here store the logger
// as a static member of this private function.
// The the SetLogger() Log() functions get this reference.
static std::unique_ptr<ILogger>& getLogReference() {
static std::unique_ptr<ILogger> logger;
return logger;
}
public:
static void SetLogger(std::unique_ptr<ILogger> new_logger) {
// Save the new reference.
getLogReference() = std::move(new_logger);
}
// Use the logger if it has been set.
static void Log(const std::string& message) {
std::unique_ptr<ILogger>& logger_ = getLogReference();
if (!logger_) {
// cannot log
} else {
logger_->OnLogEvent(message);
}
};
};
class Foo {
public:
Foo(const std::string& name) : name_{name} {
// This calls Log()
// Which calls getLogReference()
// Which forces the creation of the function static
// variable logger so it is created before this
// object is fully initialized (if it has not already
// been created).
//
// This means this object was created after the logger
LoggerWrapper::Log(name_ + ": CTOR");
}
~Foo() {
// Because the Log() function was called in the
// constructor we know the loger was fully constructed first
// thus this object will be destroyed first
// so the logger object is guaranteed to be
// available in this objects destructor
// so it is safe to use.
LoggerWrapper::Log(name_ + ": DTOR");
}
private:
std::string name_;
};
std::unique_ptr<Foo>& globalFoo() {
// foo may destroy an object created later
// that has a destructor that calls LoggerWrapper::Log()
// So we need to call the Log function here before foo
// is created.
LoggerWrapper::Log("Initializing Global foo");
// Note: Unless somebody else has explicitly called SetLogger()
// the above line is unlikely to log anything as the logger
// will be null at this point.
static std::unique_ptr<Foo> foo;
return foo;
}
int main()
{
LoggerWrapper::SetLogger(std::make_unique<ILogger>());
globalFoo() = std::make_unique<Foo>("Global FOO");
// local variables do NOT get destroyed when calling exit!
auto foo_local = Foo("Local FOO");
exit(1);
}
The order in which static (global) objects are created and destroyed is undefined.创建和销毁静态(全局)对象的顺序是未定义的。 This leave you a few options.这给你留下了几个选择。
shared_ptr
for your logger.为您的记录器使用shared_ptr
。 Each object that uses the logger gets a shared_ptr
and the logger will be cleaned up after the last object is destroyed.每个使用记录器的对象都会获得一个shared_ptr
并且在最后一个对象被销毁后记录器将被清理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.