You need to enable JavaScript to run this app. Boost Log在EXE中正常运行,作为DLL被调用时崩溃
优惠活动
大模型
产品
解决方案
定价
更多

Boost Log在EXE中正常运行,作为DLL被调用时崩溃

问题:Boost.Log在DLL中运行时抛出system_error异常

现象

  • 作为EXE独立运行时,severity_channel_logger_mt配合syslog_backend和text_file_backend两个Sink工作正常,可生成日志文件。
  • 作为DLL被主程序mainapp加载后,约20秒会抛出boost::wrapexcept<boost::system::system_error>异常,此时syslog Sink仍能传输网络日志,但text_file Sink无法生成日志。

异常信息

Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: boost::wrapexceptboost::system::system_error at memory location 0x000000F9BD9FF220.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: boost::wrapexceptboost::system::system_error at memory location 0x000000F9BD9FF220.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: boost::wrapexceptboost::system::system_error at memory location 0x000000F9BD9FF220.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFA9E03CD29 in mainapp.exe: Microsoft C++ exception: boost::wrapexceptboost::system::system_error at memory location 0x000000F9BD9FF220.

调试细节

  • 异常发生在basic_sink_frontend.hpp的backend.consume(rec, context->m_FormattedRecord);调用与core.hpp的push_record函数之间:
    BOOST_FORCEINLINE void push_record(BOOST_RV_REF(record) rec)
    {
        push_record_move(static_cast< record& >(rec));
    }
    
  • 已确认rec变量非空。
  • DLL加载无错误,可正常调用application1的函数。

环境信息

  • Boost编译为共享库,使用的库文件:boost_log_setup-vc142-mt-x64-1_80、boost_log-vc142-mt-gd-x64-1_80、boost_log-vc142-mt-x64-1_80。

代码片段

Application1代码

#define DEFAULT_PORT 13000
#define DEFAULT_PORT_STRING "13000"
#define DEFAULT_IP "127.0.0.1"

enum class severity_levels
{
    info,
    warning,
    error
};

void Application1::init_builtin_syslog()
{

    // Create a syslog sink
    boost::shared_ptr< sinks::synchronous_sink< sinks::syslog_backend > > sys_sink(
        new sinks::synchronous_sink< sinks::syslog_backend >());
    
    sys_sink->set_formatter
    (
        expr::stream << "Application 1 Log - ID: " << expr::attr< unsigned int >("RecordID")
            << "| " << expr::attr< severity_levels >("Severity") << ": " << expr::smessage
    );
    
    // Map custom levels to syslog levels
    sinks::syslog::custom_severity_mapping< severity_levels > mapping("Severity");
    mapping[severity_levels::info] = sinks::syslog::info;
    mapping[severity_levels::warning] = sinks::syslog::warning;
    mapping[severity_levels::error] = sinks::syslog::critical;
    
    sys_sink->locked_backend()->set_severity_mapper(mapping);
    
    #if !defined(BOOST_LOG_NO_ASIO)
        // Set remote address for syslog messages
        sys_sink->locked_backend()->set_target_address(DEFAULT_IP, DEFAULT_PORT);
    #endif
    
    // Create text file sink
    boost::shared_ptr< sinks::synchronous_sink< sinks::text_file_backend > > file_sink(
        new sinks::synchronous_sink< sinks::text_file_backend >(
            keywords::file_name = "application1.log",  // File name pattern
            keywords::target_file_name = "application1_%Y%m%d_%H%M%S_%2N.log",           
            keywords::rotation_size = 16384                                 
            )); 
    // Configure rotated file storage
    file_sink->locked_backend()->set_file_collector(sinks::file::make_collector(
        keywords::target = "logs",                              
        keywords::max_size = 64 * 1024 * 1024,                  
        keywords::min_free_space = 100 * 1024 * 1024,           
        keywords::max_files = 512                            
        ));
    
    // Scan for existing log files on startup
    file_sink->locked_backend()->scan_for_files();
    
    file_sink->set_formatter
    (
        expr::format("%1% Application 1 Log - ID: %2% | %3% : \"%4%\"")
        % expr::attr< boost::posix_time::ptime >("TimeStamp")
        % expr::attr< unsigned int >("RecordID")
        % expr::attr< severity_levels >("Severity")
        % expr::smessage
    );
    
    // Add sinks to logging core
    logging::core::get()->add_sink(sys_sink);
    logging::core::get()->add_sink(file_sink);
    
    // Add global logging attributes
    logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
    logging::core::get()->add_global_attribute("TimeStamp", attrs::utc_clock());

}

int start()
{
   init_builtin_syslog();
   src::severity_channel_logger_mt< severity_levels > lg(keywords::severity = severity_levels::info, keywords::channel = "Application 1");

   for (int i = 0; i < 10000; ++i)
   {
      BOOST_LOG_SEV(lg, severity_levels::info) << infoLog;
      if(runexe) std::cout << "App1 sent: " << infoLog << std::endl;
      boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
   }
return 0;
}

MainApp代码

bool loadedApp1 = false; 
std::atomic<bool> startApp1 = true;  
std::atomic<char*> app1Err((char*)""); 

HINSTANCE app1dll;
void printError( const TCHAR* msg );

typedef void (*MYPROC2)(std::atomic<bool>&, std::atomic<char*>&);
MYPROC2 app1start;

int main()
{

std::cout << "Starting application 1... " << std::endl;
        try
        {
            app1dll = LoadLibrary(TEXT("application1.dll"));
            if(app1dll == NULL)
            {
                std::cerr << "Application 1 DLL load library failure." << GetLastError() << '\n';
                loadedApp1 = false;
            }
            else
            {
                loadedApp1 = true;
                 app1start = (MYPROC2) GetProcAddress(app1dll, MAKEINTRESOURCEA(1)); 
                if (NULL != app1start) 
                {
                    std::thread t1(app1start, std::ref(startApp1), std::ref(app1Err));
                    t1.detach();
                    std::this_thread::sleep_for(std::chrono::milliseconds(4000));
                }      
                if(startApp1.load() == false)
                {
                    std::cout << "Error starting Application 1: " << app1Err.load() << std::endl;
                }
            }
}
 catch(const std::exception& e)
{
    std::cerr << e.what() << '\n';
    return 1;
}
return 0;
}

内容的提问来源于stack exchange,提问作者Derrick W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 21:05:54