Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 220
Nathan Monteleone Avatar asked Apr 24 '09 19:04

Nathan Monteleone


2 Answers

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

like image 60
Timo Geusch Avatar answered Nov 08 '22 22:11

Timo Geusch


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

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

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

like image 42
Michael Burr Avatar answered Nov 08 '22 23:11

Michael Burr