views:

674

answers:

4

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!

+1  A: 

Your code runs fine in Debug/Release mode on my VS2005. I have turned off precompiled headers and use the Multi-threaded DLL version of the runtime library.

Checked with VS2008 SP1 on a Vista machine (no precompiled headers, Multi-threaded DLL). Works just fine.

Check your install.

dirkgently
Thanks for checking 2008 too. I'm running on WinXP, not vista, although I'll be a little surprised if that makes any difference. Will check on my install.
Nathan Monteleone
+2  A: 

It looks like this a known bug in VS2005 that was fixed in VS2005 SP1:

Looks like this was submitted by the guy who posted about it on gamedev.net.

I'm not sure how or why it would have crept back into VS2008 (do you have headers from VS2005 that might be getting inthe way or something?)

Michael Burr
I do have VS2005 - I tried hiding the whole folder for it and rebuilding but I still get the same result.The gamedev guy didn't have to use ostringstream to reproduce the problem though. I wonder if it's some loophole that they didn't close.
Nathan Monteleone
I'm usually not a fan of 'compiler bugs', but that seems like a definite possiblility given the similarity (but with the ostringstream twist).
Michael Burr
+1  A: 

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).

Timo Geusch
That article gives the exact answer:"... there are two known bugs remaining in VC9, one of which can't be fixed in VC9 without breaking binary compatibility. You may be running into these bugs; however, as they are within string (and are, to our knowledge, specific to using stringstreams)..."I needed stringstream to create the problem.Thanks for your help everyone!
Nathan Monteleone
+1  A: 

I reported the problem on the Microsoft website. They acknowledged the bug and say they've fixed it for the next version.

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=435483

Nathan Monteleone