1

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;
 }
Josh
  • 834
  • 7
  • 13
  • 1
    do not use backslashes in path to header files, use slashes instead – Maxwe11 Sep 01 '12 at 18:39
  • @Josh: I am maintainer of log4cplus. Please try to run it in Visual Studio under debugger and see where is the exception being thrown. – wilx Sep 01 '12 at 19:35
  • @wilx: Tracing into the PropertyConfigurator::doConfigure call it looks like it goes south in the PropertyConfigurator constructor (the one with the 'const tstring& propertyFile' arg signature) It looks like it happens in std::basic_string::assign. Looking at the value of the propertyFile arg from the constructor, it looks like it's garbled. – Josh Sep 01 '12 at 21:34
  • FYI: I'm on Win7, using VS2010. I built log4cplus from the VS solutions without modifying any project settings. – Josh Sep 02 '12 at 14:13
  • I am able to successfully run the debug tests from the log4cplus solution (though I get the 'could not open file log4cplus.properties' error, which I expected) I've been combing through the differences and not found anything of interest yet. – Josh Sep 02 '12 at 17:29
  • Sanity check: could you tell me (or point me to documentation) of what steps I need to take to use log4cplus in my application. I added the log4cplus.lib to Project's Linker->Input->Additional_Dependencies, copied the log4cplus.dll to my working folder, and added the log4cplus include path to the C/C++->General->Additional_Include_Directories. I also made sure my project matched my log4cplus build's Character Set (multi-byte) – Josh Sep 03 '12 at 20:17
  • @Josh: That should be good enough. Are you using log4cplus DLL or log4cplus static library? – wilx Sep 08 '12 at 05:12
  • @Josh: Have you found a way to diagnose this issue? – wilx Sep 17 '12 at 12:34
  • Unfortunately no. I ended up giving up. I was creating simple Hello World applications and was never able to successfully move past the problem. Other more pressing issues came up and haven't yet revisited it. – Josh Sep 17 '12 at 16:23

3 Answers3

1

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.

FishesCycle
  • 1,011
  • 1
  • 10
  • 24
0

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;
 }
perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • I tried the code you suggested (with a slight mod, declaring 'logger' outside the TRY block) and now it is the doConfigure line that is failing, and it is the same error (bad_alloc) – Josh Sep 01 '12 at 19:08
  • I do have a log4cplus.properties file in my debug working folder as an FYI. – Josh Sep 01 '12 at 19:08
  • Another FYI: I get the same behavior whether using log4cplus-1.1.0-rc8 or log4cplus-1.0.4.1 – Josh Sep 01 '12 at 19:12
  • you tried debugging???...there may be other problems as well ...However I will suggest adding.`#include #include ` – perilbrain Sep 01 '12 at 19:15
  • I am running from the debugger, yes. It's hard to imagine that adding additional header files will change a run-time behavior. – Josh Sep 01 '12 at 19:28
  • When looking at the stack trace (on the PropertyConfigurator::doConfigure call, it looks like the value of the first argument is garbled) Could it be that LOG4CPLUS_TEXT is messed up? – Josh Sep 01 '12 at 19:35
  • I had a doubt earlier..that this all may be because of precompiled headers...and most probably it is LOG4CPLUS_TEXT got affected.. – perilbrain Sep 01 '12 at 19:37
  • If I do the following: string s = LOG4CPLUS_TEXT("asd") the value of s is "asd". – Josh Sep 01 '12 at 19:43
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!

MikeVe
  • 1,062
  • 8
  • 13