5

I have a Visual Studio 2008 C++03 application using Boost 1.47.0 running in Windows XP SP3.

The call boost::filesystem::create_directories( L"c:\\foo\\bar" ); throws a std::bad_alloc exception.

In the output window, I see a debug heap message: "HEAP[test.exe]: Invalid allocation size - CDCDCDCE (exceeded 7ffdefff)"

The callstack shows boost.filesystem creating a new locale and last visible line of code in the Microsoft standard library file xlocale line 309.

msvcp90.dll!std::_Allocate<char>()  + 0x17 bytes    
msvcp90.dll!std::allocator<char>::allocate()  + 0xf bytes    
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy()  + 0x70 bytes    
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow()  + 0x26 bytes    
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign()  + 0x50 bytes    
msvcp90.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> >()  + 0x24 bytes    
msvcp90.dll!std::locale::_Locimp::_Locimp()  + 0x47 bytes    
> test.exe!std::locale::locale<windows_file_codecvt>(const std::locale & _Loc={...}, const windows_file_codecvt * _Facptr=0x00b48f60)  Line 309 + 0x69 bytes    C++
test.exe!`anonymous namespace'::default_locale()  Line 735    C++
test.exe!`anonymous namespace'::path_locale()  Line 777 + 0x2a bytes    C++
test.exe!boost::filesystem3::path::wchar_t_codecvt_facet()  Line 797 + 0x25 bytes    C++
test.exe!boost::filesystem3::path::codecvt()  Line 388 + 0x5 bytes    C++
test.exe!boost::filesystem3::path::path<wchar_t const *>(const wchar_t * begin=0x00b460f8, const wchar_t * end=0x00b46116)  Line 162 + 0x5 bytes    C++
test.exe!boost::filesystem3::path::parent_path()  Line 313 + 0x57 bytes    C++
test.exe!boost::filesystem3::detail::create_directories(const boost::filesystem3::path & p={...}, boost::system::error_code * ec=0x00000000)  Line 832 + 0x13 bytes    C++
test.exe!boost::filesystem3::create_directories(const boost::filesystem3::path & p={...})  Line 318 + 0x29 bytes    C++
test.exe!wmain(int __formal=1, int __formal=1)  Line 112 + 0xc bytes    C++
test.exe!__tmainCRTStartup()  Line 583 + 0x19 bytes    C
test.exe!wmainCRTStartup()  Line 403    C
kernel32.dll!_BaseProcessStart@4()  + 0x23 bytes    

Can anybody suggest how to fix this issue?

EDIT I updated to boost 1.50.0. The issue remains.

PaulH
  • 7,759
  • 8
  • 66
  • 143
  • That `0xCDCDCDCE' looks like an (mostly) uninitialised data pattern to me. I'd hazard a guess that you have the source for std::basic_string in C++ standard library header files, so you can at least find out what the arguments are supposed to be for function call and compare these to those actually used by inspecting registers or the stack - even if you're linked against expansions of the template in the a pre-build library. Just to eliminate the obvious, you are using matched debug builds of everything here? – marko Aug 29 '12 at 22:09
  • @Marko: In Visual Studio debug builds, I believe uninitialized bytes are set to `0xFE` and released bytes are set to `0xCD`. – Drew Dormann Aug 29 '12 at 23:03
  • 1
    `0xCD` is the fill value used by the debug runtime for allocated memory that is uninitialized. Deleted memory is filled with `0xDD`: http://stackoverflow.com/a/370362/12711 Looks like some uninitialized allocated data item was incremented. – Michael Burr Aug 29 '12 at 23:15
  • Can you post a small repro case? A program that just consists of a `boost::filesystem::create_directories( L"c:\\foo\\bar" );` doesn't show the problem for me. Are you using VS 2008 with or with out SP1 applied? – Michael Burr Aug 30 '12 at 07:47
  • @MichaelBurr: That is exactly my repro case. Just that one line of code. Yes, I have SP1 installed. v9.0.30729.1 SP – PaulH Aug 30 '12 at 14:17
  • 1
    @MichaelBurr: I lied. My repro case was that + linking in several static libraries. Those static libraries were compiled for release mode and were linking the non-debug version of the CRT. I added `/NODEFAULTLIB:MSVCRT, msvcprt` and everything seems to work now. As a final solution, I will create debug versions of those libraries. – PaulH Aug 30 '12 at 14:42

2 Answers2

2

This seems to be a known bug in Microsoft's implementation of std::locale when running a DEBUG build. It was reported June 2012.

The message you get regarding memory address CDCDCDCE implies accessing deleted memory, as this bug describes.

There is no solution currently described in Microsoft's site, but I would suggest trying a different facet by changing L"c:\\foo\\bar" to "c:\\foo\\bar".

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

This problem smells like a mismatch in _SECURE_SCL (or maybe the somewhat related _HAS_ITERATOR_DEBUGGING - but I doubt the latter, since the call stack indicates a non-debug build). See https://stackoverflow.com/a/6104239/12711 for some info and make sure _SECURE_SCL is defined the same way in the builds for test.exe and the boost filesystem library linked in.

AFAIK, boost will use the VC default (which is _SECURE_SCL=1 even in release builds for VS2008), so if you're setting _SECURE_SCL=0 that may be the problem.

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I think this is correct. I was linking non-debug libraries to a debug build. (see my above comment) – PaulH Aug 30 '12 at 14:44