I'm attempting to implement logging in a c++ application using log4cplus. I'm able to successfully build/link (I added the log4cplus.lib to my additional libs and copied the log4cplus.dll to the build/outdir)
When I run my application, I get the following exception when it executes my Logger::getInstance call:
Unhandled exception at 0x75cad36f in LogTesterConsole.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0013ed8c..
I've tried placing the call outside my main() routine as well as inside and have the same results.
Any ideas?
Code:-
#include "stdafx.h"
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
using namespace log4cplus;
int _tmain(int argc, _TCHAR* argv[])
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
LOG4CPLUS_WARN(logger, LOG4CPLUS_TEXT("Hello, World!"));
return 0;
}
If you're building your application in debug, be sure to link to lib4CplusD.lib and lib4CplusD.dll. Likewise, a release application should link aginst lib4cplus.lib and lib4cplus.dll I had the same runtime error, and when I linked my debug application against the debug libraries, the problem was resolved.
I know this post in kinda old, but I encountered the exact same problem when I started trying out log4cplus v2.0.0 yesterday.
The short answer: It's a Debug / Release build issue.
Now for the longer answer for anyone to repruduce:
./log4cplus-2.0.0/msvc14
with VS2017 and made a release build (unicode) of thelog4cplus
-Project (Windows SDK v10.0.16299.0; Platform Toolset v141). Everything worked out fine.log4cplusU.lib
as an additional dependency to the linkerlog4cplusU.dll
to the output directory./log4cplus-2.0.0/include
to additional includesNow this is the point were I encountered the exact same problem @Josh described in his initial post. Now the actual problem is, that I made a release build of log4cplus but used this release build in a debug build of my own application.
With this is mind I made a release build of my own application and everything worked like charm!
I pushed my full VS2017-solution to my GitHub-repo, so you can reproduce the problem "hands on". Just change the configuration of this solution to "Debug" (x86) and watch it produce an exception at runtime. Change it to "Release" (x86) and watch it work like expected!
try:-