简体   繁体   English

经过全部处理后,为什么会出现错误和崩溃代码0?

[英]Why am I getting an error and crash code 0 after all was processed?

Hi I have a class for logging that uses boost and in the initLogging I have done this: 嗨,我有一个使用boost的日志记录类,在initLogging中,我这样做:

// Create a backend
boost::shared_ptr< SinkTextFileBakend > sink = boost::log::add_file_log(
    sm_folderOfLogFiles + "optimisations_%Y-%m-%d_%H-%M-%S.%N.log",
    boost::log::keywords::format = boost::log::expressions::stream
        << boost::log::expressions::attr< boost::log::attributes::current_process_name::value_type >("Executable") << "("
        << boost::log::expressions::attr< boost::log::attributes::current_process_id::value_type >("ExeUID") << ") CID("
        << correlid << ") " << severity << "["
        << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
        << "] [" << boost::log::expressions::attr< boost::log::attributes::current_thread_id::value_type >("ThreadID")
        << "] " << classname << " - " << boost::log::expressions::smessage,
    boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
    boost::log::keywords::rotation_size = 2 * 1024 * 1024,
    boost::log::keywords::auto_flush = true
);

sink->locked_backend()->set_open_handler(&openingHandler);

boost::log::core::get()->set_filter(severity >= SeverityLevels::info);
boost::log::core::get()->add_sink(sink);

// for logging in console, too
boost::shared_ptr< boost::log::sinks::text_ostream_backend > backend =
    boost::make_shared< boost::log::sinks::text_ostream_backend >();
backend->add_stream(
    boost::shared_ptr< std::ostream >(&std::cout));

// Enable auto-flushing after each log record written
backend->auto_flush(true);

// Wrap it into the frontend and register in the core.
// The backend requires synchronization in the frontend.
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > backend_sink(
    new boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend >(backend)); // for testing and printing log in sysout

backend_sink->set_formatter(
    boost::log::expressions::stream
    // line id will be written in hex, 8-digits, zero-filled
    << boost::log::expressions::attr< boost::log::attributes::current_process_name::value_type >("Executable")
    << "(" << boost::log::expressions::attr< boost::log::attributes::current_process_name::value_type >("ExeUID")
    << ") CID(" << correlid << ") " << severity << " ["
    << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
    << "] [" << boost::log::expressions::attr< boost::log::attributes::current_thread_id::value_type >("ThreadID")
    << "] " << classname << " - " << boost::log::expressions::smessage);
boost::log::core::get()->add_sink(backend_sink); // for testing and printing log in sysout

So I have 2 backends, one for writing in the file and the other for printing in the console. 因此,我有2个后端,一个用于写入文件,另一个用于在控制台中打印。 At the end of the execution I get an error like this: 在执行结束时,我会收到如下错误:

*** Error in `/home/sop/project/build/proj': double free or corruption (out): 0x00000000008caaa0 ***
*** Crashed with return code: 0 ***

(this in the IDE: KDevelop) or: (在IDE中为KDevelop):

*** Error in `./proj': double free or corruption (out): 0x00000000008caaa0 ***
Aborted (core dumped)

(if run in terminal) (如果在终端中运行)

I have done some tests and if I remove the part of logging to console, then this problem disappears. 我已经做了一些测试,如果我删除了登录控制台的部分,那么这个问题就消失了。 If it has just the part for console logging it seems that I get the same error, so it seems that all the problem is in that part. 如果它只是控制台日志记录的一部分,则似乎出现了相同的错误,因此似乎所有问题都出在该部分。 I do not understand what am I doing wrong, any help please will be appreciated. 我不明白我在做什么错,请提供任何帮助。 Thanks 谢谢

This line is problematic: 这行是有问题的:

backend->add_stream(
    boost::shared_ptr< std::ostream >(&std::cout));

std::cout is a global object and cannot be deleted with operator delete , which happens when boost::shared_ptr is destroyed. std::cout是全局对象,不能使用operator delete ,而boost::shared_ptr销毁时会发生这种情况。 You should be using boost::null_deleter : 您应该使用boost::null_deleter

backend->add_stream(
    boost::shared_ptr< std::ostream >(&std::cout, boost::null_deleter()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM