我正在使用 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"));
似乎导致了这个问题。
最佳答案
此错误 Run-Time Check Failure #2
通常是由内存中某处的错误引起的。查看您提供的示例后,您应该更改此设置:
log4cplus::PropertyConfigurator config(_T("log.properties"));
为此:
log4cplus::PropertyConfigurator configure(_T("log.properties"));
如果这没有帮助,那么开始查看内存的初始化。
关于c++ - 使用库中的代码时变量周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18898271/