Using gcc 4.6.2, make_shared() gives a useless backtrace (apparently due to some rethrow) if a constructor throws an exception. I'm using make_shared() to save a bit of typing, but this is show stopper. I've created a substitute make_shrd() that allows a normal backtrace. I'm using gdb 7.3.1.
I'm worried that:
- The bad backtrace under make_shared() is somehow my own fault
- My substitute make_shrd() will cause me subtle problems.
Here's a demo:
#include <memory>
#include <stdexcept>
using namespace std;
class foo1
{
public:
foo1( const string& bar, int x ) :m_bar(bar), m_x(x)
{
throw logic_error( "Huh?" );
}
string m_bar;
int m_x;
};
class foo2
{
public:
foo2( const string& bar, int x ) : m_foo1(bar,x)
{}
foo1 m_foo1;
};
// more debuggable substitute for make_shared() ??
template<typename T, typename... Args>
std::shared_ptr<T> make_shrd( Args... args )
{
return std::shared_ptr<T>( new T(args...));
}
int main()
{
auto p_foo2 = make_shared<foo2>( "stuff", 5 ); // debug BAD!!
// auto p_foo2 = make_shrd<foo2>( "stuff", 5 ); // debug OK
// auto p_foo2 = new foo2( "stuff", 5 ); // debug OK
// auto p_foo2 = shared_ptr<foo2>(new foo2( "stuff", 5 )); // debug OK
return (int)(long int)p_foo2;
}
Compiled with:
g++ -g -std=c++0x -Wall -Wextra main.cpp
Debugged with:
gdb a.out
The make_shared() backtrace is junk that does not show the stack to the point of the exception. All the other options provide a sane backtrace.
Thanks in advance for help and suggestions.
&&
andstd::forward<>
tomake_shrd
and it should be fine, though you're still missing out on the allocation optimization provided bystd::make_shared
. – Thimerosal