log4cxx crash after call to setFile

nrathaus
nrathaus used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
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.

Author

Commented:
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

Author

Commented:
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
Top Expert 2012
Commented:
>>Maybe the issue is caused the std::string going out of scope

That should be easy to verify, try e.g. a global variable for testing purposes.

And, '/MD' is actually what I wanted you to try ;o)
In other words, change "Use Runtime Library" to "Multithreaded DLL".

Author

Commented:
Thanks for the help, just to confirm the solution was to make it "Multi-threaded DLL" and "Use MFC in a Shared DLL"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial