读书人

boost创办log时,内存泄漏

发布时间: 2013-03-17 13:48:31 作者: rapoo

boost创建log时,内存泄漏
typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
//shared_ptr< file_sink > sink(new file_sink);

shared_ptr< file_sink > sink(new file_sink(
keywords::file_name = "logs/ServiceMonitor-%Y%m%d_%5N.log", // file name pattern
keywords::rotation_size = 1024*1024, // rotation size, in characters
//keywords::open_mode = (std::ios::out | std::ios::app),
keywords::auto_flush = true
));
// Set up where the rotated files will be stored
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = "logs", // where to store rotated files
keywords::max_size = 16 * 1024 * 1024, // maximum total size of the stored files, in bytes
keywords::min_free_space = 100 * 1024 * 1024 // minimum free space on the drive, in bytes
));

// Upon restart, scan the target directory for files matching the file_name pattern
sink->locked_backend()->scan_for_files();





// Set the log record formatter
sink->locked_backend()->set_formatter(
fmt::format("[%1%][%2%]:[%3%][%4%] - %5%")
% fmt::date_time< boost::posix_time::ptime >("TimeStamp")
% fmt::attr< unsigned int >("RecordID")
% fmt::attr< severity_level >("Severity")
% fmt::attr< boost::thread::id >("ThreadID")
% fmt::message()
);

// Add it to the core
logging::core::get()->add_sink(sink);

// Add some attributes too
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());

我用delete释放了sink,程序还是出错误,是不是其他地方还用到那内存块,真不知道怎么释放
[解决办法]
sinks 是boost哪个组件中的,没用过,学习下
[解决办法]
你用的是shared_ptr,所以对于sink来说,你添加下面的调用就可以了
logging::core::get()->remove_sink(sink);
你的标题不太厚道啊!我还以为是boost log的问题了呢,;)
[解决办法]
一个人使用工具弄破了手,很可能是人不会用这种工具;
一百个人使用工具弄破了手,可能还是人不会用这种工具;
一万个人使用工具弄破了手,几乎不太可能是人不会用这种工具;
一百万个人使用工具弄破了手,可能是这种工具不适合人用!

读书人网 >C++

热点推荐