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.
try:-
int _tmain(int argc, _TCHAR* argv[])
{
PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log4cplus.properties"));
Logger root = Logger::getRoot();
try{
Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
}
catch(...) {
cout << "Exception..." << endl;
LOG4CPLUS_FATAL(root, "Exception occured...");
}
LOG4CPLUS_WARN(logger, LOG4CPLUS_TEXT("Hello, World!"));
return 0;
}
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:
- I downloaded the newest stable release of log4cplus (v2.0.0) here
- I opend the Visual Studio Soultuin in
./log4cplus-2.0.0/msvc14
with VS2017 and made a release build (unicode) of the log4cplus
-Project (Windows SDK v10.0.16299.0; Platform Toolset v141). Everything worked out fine.
- I made a new sample C++-Solution to try out the previously build log4cplus.
- Setup to use log4cplus:
- Add
log4cplusU.lib
as an additional dependency to the linker
- Add
log4cplusU.dll
to the output directory
- Add everything in
./log4cplus-2.0.0/include
to additional includes
- Make a debug build (x86) of the sample solution. Everything worked fine.
- Having made a build of the solution I hit "run" to see if everything works.
Now 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!