[test] Linking against boost_unit_test_framework causes double-deletion in std::ostringstream

Just curious if anyone else has seen this or anything like it? Platform
is Mac OS X 10.6.1, gcc 4.2.1, boost 1.40.0 installed from MacPorts,
compiling 64-bit (default).
Here's my test code:
#include <sstream>
int main()
{
std::ostringstream str;
str << "Help Me!";
return 0;
}
Here's what I'm doing:
% g++ -o streamtest_bad -ggdb -L/opt/local/lib
-lboost_unit_test_framework-mt-d streamtest.cpp
% ./streamtest_bad
streamtest_bad(63554) malloc: *** error for object 0x7fff7008a500:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
zsh: abort ./streamtest_bad
%
Works fine against other boost libraries, eg system:
% g++ -o streamtest_good -ggdb -L/opt/local/lib -lboost_system-mt-d
streamtest.cpp
% ./streamtest_good
%
Note that this seems to be a separate problem to that reported in
ticket 3432 (https://svn.boost.org/trac/boost/ticket/3432). At least, I
have verified that the framework::clear() function isn't being called
in the above code, which does tend to point the finger at a different
part of the library.
The backtrace is not particularly enlightening, but posted here for the
sake of completeness:
(gdb) bt
#0 0x00007fff87f92ff6 in __kill ()
#1 0x00007fff88034072 in abort ()
#2 0x00007fff87f4b095 in free ()
#3 0x0000000100005533 in __gnu_cxx::new_allocator<char>::deallocate
(this=0x7fff5fbff5df, __p=0x7fff7008a500 "") at new_allocator.h:97
#4 0x0000000100005578 in std::string::_Rep::_M_destroy
(this=0x7fff7008a500, __a=@0x7fff5fbff63f) at basic_string.tcc:431
#5 0x0000000100005939 in std::string::_Rep::_M_dispose
(this=0x7fff7008a500, __a=@0x7fff5fbff63f) at basic_string.h:238
#6 0x000000010000596a in std::basic_string

2009/11/10 Alastair Rankine
#include <sstream>
int main() { std::ostringstream str; str << "Help Me!";
return 0; }
Here's what I'm doing:
% g++ -o streamtest_bad -ggdb -L/opt/local/lib -lboost_unit_test_framework-mt-d streamtest.cpp
As far as I remember, boost_unit_test_framework defines main function. Having two main functions in the same application might be what's causing your problems. Another possibility is that the library and the program are compiled with different standard libraries. Roman Perepelitsa.

Roman Perepelitsa wrote:
2009/11/10 Alastair Rankine
Just curious if anyone else has seen this or anything like it? Platform is Mac OS X 10.6.1, gcc 4.2.1, boost 1.40.0 installed from MacPorts, compiling 64-bit (default). I have crashes and "double deletions" in my unit tests, too. I am running Linux, gcc 4.2.1, Boost 1.41.0-beta1; (I have no crashes or double deletions with Boost 1.40.0.)
As far as I remember, boost_unit_test_framework defines main function. Having two main functions in the same application might be what's causing your problems. Definitely no two mains; 'my' main comes from a macro provided by Boost.Test.
Another possibility is that the library and the program are compiled with different standard libraries. I have built both boost and my programs/tests myself, so I can rule the "different standard library" thing out.
Best regards

On 2009-11-10 23:48:49 +1100, Roman Perepelitsa
As far as I remember, boost_unit_test_framework defines main function. Having two main functions in the same application might be what's causing your problems. Another possibility is that the library and the program are compiled with different standard libraries.
Good thoughts, but: 1. Boost Test does not define main unless you tell it to, and that's only in a header. The library itself doesn't contain main: % nm /opt/local/lib/libboost_unit_test_framework-mt-d.dylib |c++filt|grep main 00000000000759e6 T boost::runtime::cla::argv_traverser::remainder(int&, char**) 000000000002e26a T boost::unit_test::unit_test_main(bool (*)(), int, char**) 0000000000136670 S typeinfo for std::domain_error 00000000000b2a0a S typeinfo name for std::domain_error % 2. I compiled the library myself, or at least MacPorts did, using the same standard library. I'm pretty sure it's caused by the unit test library doing something dodgy at static initialisation time. Though I don't know exactly what... Mainly I'm interested in whether anyone else can replicate this problem.

On 2009-11-10 21:50:20 +1100, Alastair Rankine
Just curious if anyone else has seen this or anything like it? Platform is Mac OS X 10.6.1, gcc 4.2.1, boost 1.40.0 installed from MacPorts, compiling 64-bit (default).
% g++ -o streamtest_bad -ggdb -L/opt/local/lib -lboost_unit_test_framework-mt-d streamtest.cpp % ./streamtest_bad streamtest_bad(63554) malloc: *** error for object 0x7fff7008a500: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug zsh: abort ./streamtest_bad %
OK here's the fix for whoever is interested. Basically it seems to be an instance of this problem: http://www.newartisans.com/2009/10/a-c-gotcha-on-snow-leopard.html (and it's not a double-deletion, it's trying to delete an object on the stack). The root cause seems to be system headers not matching the libraries, and the fix is to rebuild with -D_GLIBCXX_FULLY_DYNAMIC_STRING. I'm still not entirely sure why this bug seemed to be triggered by linking to the unit test framework library, if anyone has any ideas I'd be interested to hear them.

On 2009-11-15 01:04:44 +1100, Alastair Rankine
On 2009-11-10 21:50:20 +1100, Alastair Rankine
said: Just curious if anyone else has seen this or anything like it? Platform is Mac OS X 10.6.1, gcc 4.2.1, boost 1.40.0 installed from MacPorts, compiling 64-bit (default).
% g++ -o streamtest_bad -ggdb -L/opt/local/lib -lboost_unit_test_framework-mt-d streamtest.cpp % ./streamtest_bad streamtest_bad(63554) malloc: *** error for object 0x7fff7008a500: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug zsh: abort ./streamtest_bad %
OK here's the fix for whoever is interested. Basically it seems to be an instance of this problem: http://www.newartisans.com/2009/10/a-c-gotcha-on-snow-leopard.html (and it's not a double-deletion, it's trying to delete an object on the stack).
The root cause seems to be system headers not matching the libraries, and the fix is to rebuild with -D_GLIBCXX_FULLY_DYNAMIC_STRING.
Not quite. It's actually the debug variant of boost in MacPorts. This silently enables _GLIBCXX_DEBUG for the build, but which is also required in your own applications. More info in the ticket here: http://trac.macports.org/ticket/22112#comment:2

Alastair Rankine
The root cause seems to be system headers not matching the libraries, and the fix is to rebuild with -D_GLIBCXX_FULLY_DYNAMIC_STRING.
Not quite.
It's actually the debug variant of boost in MacPorts.
This silently enables _GLIBCXX_DEBUG for the build, but which is also required in your own applications.
More info in the ticket here: http://trac.macports.org/ticket/22112#comment:2
Does this mean that the issue is resolved? Thx, Gennadiy
participants (4)
-
Alastair Rankine
-
Christoph Duelli
-
Gennadiy Rozental
-
Roman Perepelitsa