0

I'm using log4cplus as a logger for both CLR and non-CLR C++/CLI code and C# code so for that reason I'm using the Unicode x64 build of log4cplus, log4cplusU.lib/dll.

If I run the following code in a non-CLR C++/CLI x64 console application, I get a memory access exception.

int _tmain(int argc, _TCHAR* argv[])
{
    std::string LogFileName = "log4cplus.log";
    auto db = log4cplus::helpers::towstring(LogFileName);

Exception:

Unhandled exception at 0x00007FF8E4A1CDA1 (msvcr120.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

What's up?

I'm using Visual Studio 2013. My call stack at the exception looks like:

>   log4cplusU.dll!std::vector<wchar_t,std::allocator<wchar_t> >::vector<wchar_t,std::allocator<wchar_t> >(unsigned __int64 _Count) Line 691    C++
    log4cplusU.dll!log4cplus::helpers::towstring_internal(std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > & outstr, const char * src, unsigned __int64 size, const std::locale & loc) Line 70 C++
    log4cplusU.dll!log4cplus::helpers::towstring(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & src) Line 124 C++
    ConsoleApplication1.exe!wmain(int argc, wchar_t * * argv) Line 24   C++
    ConsoleApplication1.exe!__tmainCRTStartup() Line 623    C

At the point where the exception fires in std::vector(size_type), _Count is a crazy number.

_Count  14757396612626683276    unsigned __int64

The reason appears to be that the string parameter gets scrambled or misinterpreted.

The same problem manifests itself on non-Unicode DEBUG MODE builds of log4cplus in unmanaged code on VS but not in release mode builds.

For example:

#include <string>
#include "stdafx.h"

#include <iostream>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>

int _tmain(int argc, _TCHAR* argv[])
{
    std::string LogConfigFileName = "WhenLoggingCppManagedCode.properties";

    try
    {
        log4cplus::tstring cfn = LogConfigFileName;
        log4cplus::PropertyConfigurator::doConfigure(cfn);

        std::cout << "Good Deadpool." << std::endl;
    }
    catch (...)
    {
        std::cout << "BAD Deadpool." << std::endl;
    }

    std::cin.get();
    return 0;
}
empty
  • 5,194
  • 3
  • 32
  • 58
  • A function like towstring() is very dangerous, a template class like std::wstring can only ever work if the library was compiled with the exact same compiler version with the exact same settings that you used in your main project. Seeing a crash in the release build CRT instead of the debug version (msvcr120d.dll) strongly suggests that you have a mismatch. You'll have to rebuild the library. – Hans Passant May 16 '16 at 17:23
  • @HansPassant trouble persists even with a rebuild of the library. – empty May 16 '16 at 17:42
  • You'll have to provide more info, at least the VS version you use and the content of the Call Stack window when the exception occurs. – Hans Passant May 16 '16 at 17:44
  • @HansPassant Done. Thanks. – empty May 16 '16 at 18:36

0 Answers0