unique_ptr 的 CRTP 导致段错误
CRTP with unique_ptr causes segfault
我正在使用 CRTP 设计模式为我的项目实现日志记录机制。基本 CRTP class 看起来像这样:
#include <fstream>
#include <memory>
#include <mutex>
#include <iostream>
#include <sstream>
template <typename LogPolicy>
class Logger
{
public:
template <typename... Args>
void operator()(Args... args)
{
loggingMutex.lock();
putTime();
print_impl(args...);
}
void setMaxLogFileSize(unsigned long maxLogFileSizeArg)
{
//if (dynamic_cast<FileLogPolicy *>(policy.get()))
// policy->setMaxLogFileSize(maxLogFileSizeArg);
}
~Logger()
{
print_impl(END_OF_LOGGING);
}
protected:
std::stringstream buffer;
std::mutex loggingMutex;
std::string d_time;
private:
static constexpr auto END_OF_LOGGING = "***END OF LOGGING***";
void putTime()
{
time_t raw_time;
time(&raw_time);
std::string localTime = ctime(&raw_time);
localTime.erase(std::remove(localTime.begin(), localTime.end(), '\n'), localTime.end());
buffer << localTime;
}
template <typename First, typename... Rest>
void print_impl(First first, Rest... rest)
{
buffer << " " << first;
print_impl(rest...);
}
void print_impl()
{
static_cast<LogPolicy*>(this)->write(buffer.str());
buffer.str("");
}
};
其中一个具体的日志记录 class 是记录到文件,它看起来像这样:
#include "Logger.hpp"
class FileLogPolicy : public Logger<FileLogPolicy>
{
public:
FileLogPolicy(std::string fileName) : logFile(new std::ofstream)
{
logFile->open(fileName, std::ofstream::out | std::ofstream::binary);
if (logFile->is_open())
{
std::cout << "Opening stream with addr " << (logFile.get()) << std::endl;
}
}
void write(const std::string content)
{
std::cout << "Writing stream with addr " << (logFile.get()) << std::endl;
(*logFile) << " " << content << std::endl;
loggingMutex.unlock();
}
virtual ~FileLogPolicy()
{
}
private:
std::unique_ptr<std::ofstream> logFile; //Pointer to logging stream
static const char *const S_FILE_NAME; //File name used to store logging
size_t d_maxLogFileSize; //File max size used to store logging
};
基本上我创建了策略对象 class 并希望根据所选策略记录内容。因此,例如我这样创建记录器:
FileLogPolicy log("log.txt");
在这种情况下,它应该通过调用 static_cast<LogPolicy*>(this)->write(buffer.str())
使用 Logger 将日志保存到文件。显然调用 write 函数工作正常,但流对象正在更改为 null。如果尚未调用 FileLogPolicy 析构函数,那怎么可能呢?当我将 logFile 更改为普通指针时,一切正常。我不明白有什么不同。
~Logger()
{
print_impl(END_OF_LOGGING);
}
此代码在后代 class 被销毁后运行。
void print_impl()
{
static_cast<LogPolicy*>(this)->write(buffer.str());
buffer.str("");
}
然后它将 this
转换为指向 class 的指针,而 this
不再存在。
唯一ptr没有了,连访问会员都是UB
我正在使用 CRTP 设计模式为我的项目实现日志记录机制。基本 CRTP class 看起来像这样:
#include <fstream>
#include <memory>
#include <mutex>
#include <iostream>
#include <sstream>
template <typename LogPolicy>
class Logger
{
public:
template <typename... Args>
void operator()(Args... args)
{
loggingMutex.lock();
putTime();
print_impl(args...);
}
void setMaxLogFileSize(unsigned long maxLogFileSizeArg)
{
//if (dynamic_cast<FileLogPolicy *>(policy.get()))
// policy->setMaxLogFileSize(maxLogFileSizeArg);
}
~Logger()
{
print_impl(END_OF_LOGGING);
}
protected:
std::stringstream buffer;
std::mutex loggingMutex;
std::string d_time;
private:
static constexpr auto END_OF_LOGGING = "***END OF LOGGING***";
void putTime()
{
time_t raw_time;
time(&raw_time);
std::string localTime = ctime(&raw_time);
localTime.erase(std::remove(localTime.begin(), localTime.end(), '\n'), localTime.end());
buffer << localTime;
}
template <typename First, typename... Rest>
void print_impl(First first, Rest... rest)
{
buffer << " " << first;
print_impl(rest...);
}
void print_impl()
{
static_cast<LogPolicy*>(this)->write(buffer.str());
buffer.str("");
}
};
其中一个具体的日志记录 class 是记录到文件,它看起来像这样:
#include "Logger.hpp"
class FileLogPolicy : public Logger<FileLogPolicy>
{
public:
FileLogPolicy(std::string fileName) : logFile(new std::ofstream)
{
logFile->open(fileName, std::ofstream::out | std::ofstream::binary);
if (logFile->is_open())
{
std::cout << "Opening stream with addr " << (logFile.get()) << std::endl;
}
}
void write(const std::string content)
{
std::cout << "Writing stream with addr " << (logFile.get()) << std::endl;
(*logFile) << " " << content << std::endl;
loggingMutex.unlock();
}
virtual ~FileLogPolicy()
{
}
private:
std::unique_ptr<std::ofstream> logFile; //Pointer to logging stream
static const char *const S_FILE_NAME; //File name used to store logging
size_t d_maxLogFileSize; //File max size used to store logging
};
基本上我创建了策略对象 class 并希望根据所选策略记录内容。因此,例如我这样创建记录器:
FileLogPolicy log("log.txt");
在这种情况下,它应该通过调用 static_cast<LogPolicy*>(this)->write(buffer.str())
使用 Logger 将日志保存到文件。显然调用 write 函数工作正常,但流对象正在更改为 null。如果尚未调用 FileLogPolicy 析构函数,那怎么可能呢?当我将 logFile 更改为普通指针时,一切正常。我不明白有什么不同。
~Logger()
{
print_impl(END_OF_LOGGING);
}
此代码在后代 class 被销毁后运行。
void print_impl()
{
static_cast<LogPolicy*>(this)->write(buffer.str());
buffer.str("");
}
然后它将 this
转换为指向 class 的指针,而 this
不再存在。
唯一ptr没有了,连访问会员都是UB