Link to home
Start Free TrialLog in
Avatar of nrathaus
nrathausFlag for United States of America

asked on

log4cxx crash after call to setFile

Hi,

I am trying to use the following code in Visual Studio 2010, under the previous version of Visual Studio it worked without a hitch, now it crashes:
LogString strLayout(LOG4CXX_STR("[%d{yyyy-MM-dd HH:mm:ss}, %p]%m%n"));
log4cxx::PatternLayoutPtr FileLayout = new log4cxx::PatternLayout(strLayout);

log4cxx::RollingFileAppenderPtr pFileAppMain(new log4cxx::RollingFileAppender());

pFileAppMain->setLayout(FileLayout);
pFileAppMain->setFile(log4cxx::helpers::Transcoder::decode(m_Filename.c_str()));

Open in new window


m_Filename is set as:
static std::string CLogger::m_Filename = "";

And assigned a value of "c:\LOGLOCATION\" just before the above code is called.

The code variable is set as static as the CLogger code is called by several functions, so I want the log object to be shared.

This is the crash:
        GUI.exe!_CrtIsValidHeapPointer(const void * pUserData)  Line 2036	C++
 	GUI.exe!_free_dbg_nolock(void * pUserData, int nBlockUse)  Line 1322 + 0x9 bytes	C++
 	GUI.exe!_free_dbg(void * pUserData, int nBlockUse)  Line 1265 + 0xd bytes	C++
 	GUI.exe!operator delete(void * p)  Line 347 + 0xb bytes	C++
 	GUI.exe!std::allocator<wchar_t>::deallocate(wchar_t * _Ptr, unsigned int __formal)  Line 182 + 0x9 bytes	C++
 	GUI.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::_Tidy(bool _Built, unsigned int _Newsize)  Line 1998	C++
 	GUI.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::~basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >()  Line 755	C++
 	GUI.exe!CLogger::initLog4cxx(std::basic_string<char,std::char_traits<char>,std::allocator<char> > logThreads, std::basic_string<char,std::char_traits<char>,std::allocator<char> > logExceptions)  Line 95	C++

Open in new window


I believe the problem is because std::string is going out of context and the log4cxx hasn't made a copy of it.
Avatar of jkr
jkr
Flag of Germany image

If you are actually receiving an "assertion failed" on the line

_CrtIsValidHeapPointer(const void * pUserData)

this would rather point to memory being deallocated in a module other than where it was allocated. Is your pproject set to use the CRT as a shared library? If not, try to set that.

Avatar of nrathaus

ASKER

Hi,

The project is configured as:
Use MFC in a Static Library

I am not sure I can convert the project to run as a Shared Library.


Maybe the issue is caused the std::string going out of scope, but I can't find any log4cxx documentation on how to properly pass a std::string to a LogString object
Trying to change to:
Use MFC in a Shared DLL

Causes the following error to appear immediately as I start compilation:
fatal error C1189: #error :  Please use the /MD switch for _AFXDLL builds
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help, just to confirm the solution was to make it "Multi-threaded DLL" and "Use MFC in a Shared DLL"