了解valgrind的输出

时间:2012-09-06 21:51:45

标签: c++ linux memory-leaks valgrind

我正在为大学写一个项目,一切都完成了,通过了所有测试,运行正常,但是valgrind告诉我这个:

==8059== Invalid read of size 8
==8059==    at 0x406E4E: RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)
==8059==    by 0x403368: MyPoly::operator+(MyPoly const&) const (MyPoly.cpp:281)
==8059==    by 0x403A6D: MyPoly::operator+=(MyPoly const&) (MyPoly.cpp:354)
==8059==    by 0x401E20: main (DemoPoly.cpp:50)
==8059==  Address 0x5953f50 is 0 bytes after a block of size 16 alloc'd
==8059==    at 0x4C27297: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8059==    by 0x4060CD: __gnu_cxx::new_allocator<double>::allocate(unsigned long, void const*) (new_allocator.h:92)
==8059==    by 0x405934: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned long) (in /a/fr-05/vol/home/stud/lablabla/CppLab/Ex3/DemoPoly)
==8059==    by 0x407355: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned long, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (stl_vector.h:1052)
==8059==    by 0x40700E: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (vector.tcc:167)
==8059==    by 0x406C5D: RegPoly::RegPoly(RegPoly const&) (RegPoly.cpp:19)
==8059==    by 0x4031DB: MyPoly::operator=(MyPoly const&) (MyPoly.cpp:249)
==8059==    by 0x403B58: MyPoly::operator*=(MyPoly const&) (MyPoly.cpp:364)
==8059==    by 0x401DB6: main (DemoPoly.cpp:44)

行:

44 - p3 *= p2; // both are MyPoly objects

50 - p3 += p1;

我有点明白,这意味着我正在尝试从已经改变的内存中读取(?)但我无法理解为什么。 我可以发布相关代码,但是,我不确定哪个部分,因为它会使问题变得混乱。我可以粘贴相关部分,告诉我你需要什么。

谢谢!

编辑: 这是代码:

MyPoly& MyPoly::operator *=(const MyPoly& rhs)
{
    *this = *this * rhs; // 364
    return *this;
}

===================

case PolyInterface::REG:
{
    RegPoly *tempReg = dynamic_cast<RegPoly*>(rhs.p_PolyBody); // rhs is an interface, hence the dynamic cast
    if (tempReg != NULL)
    {
        p_PolyBody = new RegPoly(*tempReg);  // 249. p_PolyBody is a pointer stored in MyPoly. points to RegPoly object
    }
    break;
}

===================

RegPoly::RegPoly(RegPoly const& other)
{
    gCurrentRank = 0;
    gData = other.gData; // 19. gData is a vector<double>
    _isZeroPoly = other._isZeroPoly;
}

===================

double RegPoly::getCurrentCoefficient() const
{
    return *gDataIterator; // 59. vector<double>::const_iterator
}

===================

newPolyValues.push_back(
                p_PolyBody->getCurrentCoefficient() + rhs.p_PolyBody->getCurrentCoefficient());
// 281.

编辑: 当getCurrentCoefficient()返回gData[gCurrentRank]时,我也会收到此消息 这意味着它与gData本身的位置有关,对吧?

RegPoly:

std::vector<double> gData;
std::vector<double>::iterator gDataIterator;
int gCurrentRank;
bool _isZeroPoly; // Inherited from the interface

2 个答案:

答案 0 :(得分:1)

信任valgrind,它几乎总是显示存在的问题。

以下是阅读邮件的方式:

RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)尝试从内存位置读取它不应该读取的内容,因为它不属于您的应用程序(未分配,非堆栈等等)。这个无效地址超出了使用新操作符分配的空间,在RegPoly复制构造函数中使用(您可以看到创建该调用的整个回溯)。

如果这还不够,请发布相关代码。

更新

我没有看到您的复制构造函数复制gDataIterator(复制时为btw,为什么要将gCurrentRank初始化为0?)。

答案 1 :(得分:1)

问题在于RegPoly.cpp:59不在DemoPoly.cpp:50中。

据说您已经分配了16个字节,但是尝试读取超出分配的8个字节。

请参阅:http://valgrind.org/docs/manual/mc-manual.html#mc-manual.errormsgs

修改

可能您正在尝试访问无效的迭代器。 const迭代器只是意味着您无法使用此迭代器来修改容器内的值。

修改2

与无效迭代器相关的brief explanation