http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56166
Bug #: 56166
Summary: std::string::clear() can allocate memory despite being
marked noexcept
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: [email protected]
ReportedBy: [email protected]
The attached test case (which is regrettably large, but I haven't found a
shorter way to trigger the problem) will call terminate() when compiled with
-std=c++11, despite there being no point at which an exception should escape
main(). The problem is deep inside std::basic_string:
/usr/include/c++/4.7/bits/basic_string.h
796 /**
797 * Erases the string, making it empty.
798 */
799 void
800 clear() _GLIBCXX_NOEXCEPT
801 { _M_mutate(0, this->size(), 0); }
_M_mutate can allocate memory even when it's being asked to erase the string
(presumably due to internal reference-counting -- if you take 's2' out of the
test case the crash goes away), and thus can throw bad_alloc. In C++11 mode,
clear() is marked 'noexcept', so we wind up in terminate().