使用库中的代码时,变量周围的堆栈已损坏

时间:2013-09-19 15:02:40

标签: c++ visual-studio stack corruption log4cplus

我正在使用log4cplus库。当我构建应用程序时,它编译并正常运行(好吧,不太正确,因为它没有记录任何东西,但那是另一个问题),但当我关闭它时,我得到了这个错误:

Run-Time Check Failure #2 - Stack around the variable 's1' was corrupted.

这是我的代码。我用评论标记了相关的地方。

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                       _In_opt_ HINSTANCE hPrevInstance,
                       _In_ LPTSTR    lpCmdLine,
                       _In_ int       nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

    ////////////////// SET UP CHECKS FOR MEMORY LEAKS ////////////////////
    _CrtMemState s1;
    _CrtMemCheckpoint(&s1);
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    //////////////////////////////////////////////////////////////////////

    log4cplus::PropertyConfigurator config(_T("log.properties")); // <-- this line seems to be responsible for the issue. When I remove it, everything is ok.

    _CrtMemDumpAllObjectsSince(&s1); // <-- here program breaks with mentioned error.

    return 1;
}

因此,正如在评论中所写,PropertyConfigurator()构造函数似乎应该对问题负责。此处没有任何其他代码导致相同的问题。

我想知道如果这个库被许多人使用并且它有效,那么可能会出错,而我有堆栈损坏的问题。

有没有人知道这里发生了什么?

编辑:

我删除了所有不必要的代码(上面的代码已编辑),只留下相关代码。仍然log4cplus::PropertyConfigurator config(_T("log.properties"));似乎导致了这个问题。

1 个答案:

答案 0 :(得分:1)

此错误Run-Time Check Failure #2通常是由内存中的某个错误引起的。在查看您提供的示例后,您应该更改此内容:

log4cplus::PropertyConfigurator config(_T("log.properties"));

到此:

log4cplus::PropertyConfigurator configure(_T("log.properties"));

如果这没有用,那么就开始查看内存的初始化。