valgrind 无效读取大小 4,错误在哪里?
我是 valgrind 的新手,我正在尝试使用它来解决内存泄漏和其他内存问题。在我的程序中,我定义了以下类
class LM_EXPORT LdpElement : public Visitable, virtual public RefCounted,
public NodeInTree<LdpElement>
{
protected:
ELdpElement m_type; // the element type
std::string m_name; // for composite: element name
std::string m_value; // for simple: the element value
bool m_fSimple; // true for simple elements
int m_numLine; // file line in which the element starts or 0
long m_id; // for composite: element ID (0..n)
ImoObj* m_pImo;
LdpElement();
public:
virtual ~LdpElement();
//getters and setters
...
inline ImoObj* get_imo() { return m_pImo; }
Valgrind 在最后一行中抱怨“大小 4 的读取无效”。为什么?返回指针的内存问题出在哪里?
I'm new with valgrind and I'm trying to use it for memory leaks and other memory problems. In my program I have defined the following class
class LM_EXPORT LdpElement : public Visitable, virtual public RefCounted,
public NodeInTree<LdpElement>
{
protected:
ELdpElement m_type; // the element type
std::string m_name; // for composite: element name
std::string m_value; // for simple: the element value
bool m_fSimple; // true for simple elements
int m_numLine; // file line in which the element starts or 0
long m_id; // for composite: element ID (0..n)
ImoObj* m_pImo;
LdpElement();
public:
virtual ~LdpElement();
//getters and setters
...
inline ImoObj* get_imo() { return m_pImo; }
Valgrind is complaining in this last line "invalid read of size 4". Why? Where is the memory problem in returning the pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许 m_pImo 没有初始化并且 valgrind 不喜欢这样?您的构造函数的实现是否有一个初始化
m_pImo
的初始化列表?Perhaps
m_pImo
isn't initialized and valgrind doesn't like that? Does the implementation of your constructor have an initialization list that initializesm_pImo
?在很高的层面上,而且不一定总是原因,我发现有时 Valgrind 会抱怨,所以当你的最终(调试)二进制文件(可执行文件)中有多个符号时(当链接器选项设置为 false 时,“死代码剥离”)。
我希望这能在未来帮助其他人。我在浪费了两个小时后才发现这一点。 :(
At a high very level, and not necessarily always the cause, I have seen that sometimes Valgrind will complain so when your final (debug) binary (executable) has more than one symbol in it (when linker option is set to be false for "dead code stripping").
I hope this will help others in future. I just discovered this after wasting 2 hours on it. :(