4

I'm using log4cplus library. When I build application, it compiles and runs properly (well, not quite properly since it's not logging anything, but that's the other issue), but when I close it, I'm getting this error:

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

Here is my code. I marked relevant places with comments.

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;
}

So, as written in comments, PropertyConfigurator() constructor seems to be responsible for the problem. Not any other code in this place causes the same problem.

I wonder what could be wrong if this library is used by many people and it works, while I have problems with stack corruption.

Does anyone have any idea of what's going on in here?

EDIT:

I removed all unnecessary code (the code above is edited) and left only the relevant. Still log4cplus::PropertyConfigurator config(_T("log.properties")); seems to cause the issue.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65

1 Answers1

1

This error Run-Time Check Failure #2 is generally caused by an error somewhere in memory. After looking at the sample you provided you should change this:

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

to this:

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

If that does not help then start looking at initializations of memory.

Bmize729
  • 1,126
  • 8
  • 18