C++ – Problem when disabling checked iterators in vs2008 SP1 (_HAS_ITERATOR_DEBUGGING=0)

c++crashdebuggingiteratorvisual-studio-2008

I've been having some trouble with vs2008 SP1 running in debug mode when I try to disable checked iterators. The following program reproduces the problem (a crash in the string destructor):

#define _HAS_ITERATOR_DEBUGGING 0

#include <sstream>

int do_stuff(std::string const& text)
{
    std::string::const_iterator i(text.end());
    return 0;
}

int main()
{
    std::ostringstream os;
    os << "some_text";
    return do_stuff(os.str());
}

I'd found a similar post on gamdev.net that discussed having this problem in vs2005. The example program in that post compiles for me on 2008 SP1 as is – but when I modded it to use ostringstream, I was able to get the problem.

From poking around in the debugger, it looks like the library pops iterators off the stack, then later tries to use them in _Orphan_All, which is some kind of iterator checking cleanup code…

Can anyone else reproduce this problem or tell me what's going on?

Thanks!

Best Solution

I've just tried this in VS2008 on Windows XP and got a warning regarding a buffer overflow, both on a pre- and a post-SP1 VS2008.

Interestingly enough the problem seems to be centred around passing the string into do_stuff either by reference or by value - if I use the original code, it complains about the buffer overflow but if I pass the string in by value, it runs fine. This is with the multithreaded debug DLL runtime. The error disappears when you like against the static MT Debug runtime.

In both cases, precompiled headers were turned off and the files that normally generate the precompiled headers have been removed from the project.

After reading this article on MSDN I'm wondering if the problem stems from the fact that several C++ standard library classes are actually residing in the runtime library if you build with the debug DLL runtimes (just try to link a VS2008-generated binary against an earlier library and watch out for the unresolved externals to confirm this).