make_shared() 的可调试替换
使用 gcc 4.6.2,如果构造函数抛出异常,make_shared() 会给出无用的回溯(显然是由于某些重新抛出)。我使用 make_shared() 来节省一些打字时间,但这是表演的阻碍。我创建了一个替代的 make_shrd() ,它允许正常的回溯。我正在使用 gdb 7.3.1。
我担心:
- make_shared() 下的错误回溯在某种程度上是我自己的错
- 我的替代品 make_shrd() 会给我带来微妙的问题。
这是一个演示:
#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;
}
编译方式:
g++ -g -std=c++0x -Wall -Wextra main.cpp
调试方式:
gdb a.out
make_shared() 回溯是垃圾,不会显示到异常点的堆栈。所有其他选项都提供合理的回溯。
预先感谢您的帮助和建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
make_shrd()
的实现失去了只分配一块内存的能力:std::make_shared()
做了两件事:std::make_shared()
的主要目的实际上是第二个功能 我没有看过实现,但我怀疑这也是真正给你带来问题的部分。除此之外,一旦修复参数转发,我不认为您的实现会变得更糟:Your implementation of
make_shrd()
looses the ability to allocate just one chunk of memory:std::make_shared()
does two things:std::shared_ptr<T>
are the same rather than the latter being for a base class)The main purpose of
std::make_shared()
is actually the second feature. I haven't looked at the implementation but I suspect that this is also the part which actually causes you problems. Other than that, I don't see any reason why your implementation is any worse once you fix forwarding of arguments: