Seb*_*ian 23 c++ debugging boost gdb shared-ptr
我还没有参与共享指针..我只知道这个概念.我正在尝试调试以下c ++类中的函数,该类存储XML文件的数据(通过xerces库读入).
// header file
class ParamNode;
typedef boost::shared_ptr<ParamNode> PtrParamNode;
class ParamNode : public boost::enable_shared_from_this<ParamNode> {
public:
...
typedef enum { DEFAULT, EX, PASS, INSERT, APPEND } ActionType;
bool hasChildren() const;
PtrParamNode GetChildren();
PtrParamNode Get(const std::string& name, ActionType = DEFAULT );
protected:
....
ActionType defaultAction_;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我正在调试一段代码,其中我有一个指向该类的指针的实例ParamNode,并且它被调用paramNode_
PtrParamNode paramNode_;
// read xml file with xerces
paramNode_ = xerces->CreateParamNodeInstance();
// now, the xml data is stored in paramNode_.
std::string probGeo;
// this works in the code, but not in (gdb)!!
paramNode_->Get("domain")->GetValue("gt",probGeo);
cout << probGeo << endl; // <-- breakpoint HERE
Run Code Online (Sandbox Code Playgroud)
使用gdb我正在检查paramNode_对象:
(gdb) p paramNode_
$29 = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}
(gdb) p *paramNode_.px
$30 = {
<boost::enable_shared_from_this<mainclass::ParamNode>> = {weak_this_ = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}},
_vptr.ParamNode = 0x20d5ad0 <vtable for mainclass::ParamNode+16>,
...
name_= {...},
children_ = {size_ = 6, capacity_ = 8, data_ = 0x2969798},
defaultAction_ = mainclass::ParamNode::EX, }
Run Code Online (Sandbox Code Playgroud)
并打印其成员:
(gdb) ptype *paramNode_.px
type = class mainclass::ParamNode : public boost::enable_shared_from_this<mainclass::ParamNode> {
protected:
...
mainclass::ParamNode::ActionType defaultAction_;
public:
bool HasChildren(void) const;
mainclass::PtrParamNode GetChildren(void);
mainclass::PtrParamNode Get(const std::string &, mainclass::ParamNode::ActionType);
Run Code Online (Sandbox Code Playgroud)
但是,我只能调用函数HasChildren或GetChildren,而调用Get从gdb一个错误的结果:
(gdb) p (paramNode_.px)->HasChildren()
$7 = true
(gdb) p (paramNode_.px)->GetChildren()
$8 = (mainclass::ParamNodeList &) @0x295dfb8: {
size_ = 6,
capacity_ = 8,
data_ = 0x29697a8
}
(gdb) p (paramNode_.px)->Get("domain")
Cannot resolve method mainclass::ParamNode::Get to any overloaded instance
(gdb) set overload-resolution off
(gdb) p (paramNode_.px)->Get("domain")
One of the arguments you tried to pass to Get could not be converted to what the function wants.
(gdb) p (paramNode_.px)->Get("domain", (paramNode_.px).defaultAction_)
One of the arguments you tried to pass to Get could not be converted to what the function wants.
Run Code Online (Sandbox Code Playgroud)
在代码中,执行该Get("domain")函数工作得很好.这是为什么?如果你在答案中包含解释,我很感谢,因为我对共享指针的了解有限.
n. *_* m. 28
gdb它不是一个编译器,它不会为你做(不那么)好的用户定义类型转换.如果你想调用一个想要a的函数string,你需要给它一个string,而不是一个const char*.
遗憾的是,gdb无法std::string在命令行上为您构造一个,因为它不是编译器而且对象创建不是简单的函数调用.
所以你必须为你的程序添加一个小帮助函数,这需要一个const char*并返回一个std::string&.请注意这里的参考.它不能按值返回,因为那时gdb将无法通过const引用传递结果(它不是编译器!)您可以选择返回对静态对象的引用,或者返回堆上分配的对象.在后一种情况下,它会泄漏内存,但这不是什么大问题,因为该函数只能从调试器中调用.
std::string& SSS (const char* s)
{
return *(new std::string(s));
}
Run Code Online (Sandbox Code Playgroud)
然后进去 gdb
gdb> p (paramNode_.px)->Get(SSS("domain"))
Run Code Online (Sandbox Code Playgroud)
应该管用.
rws*_*wst 12
在这种情况下,我只是在发出命令后取得了成功
set overload-resolution off
Run Code Online (Sandbox Code Playgroud)
Tom*_*mey 10
前一个答案的几个补充 -
gdb最终可能会学习如何进行这样的转换.它现在不能; 但是正在积极努力改进对C++表达式解析的支持.
gdb也不了解默认参数.这部分是gdb中的一个错误; 但也部分是g ++中的一个错误,它不会将它们发送到DWARF中.我认为DWARF甚至没有定义一种发出非平凡的默认参数的方法.
小智 5
nm 的回答很好,但为了避免编辑代码并重新编译,请查看在 GDB中创建 C++ 字符串中给出的解决方案。
在那个解决方案中,他们演示了std::string在堆上为 a 分配空间,然后初始化 astd::string以传递到function他们想要从 调用的gdb。
| 归档时间: |
|
| 查看次数: |
16857 次 |
| 最近记录: |