了解瓦尔格林德的输出
Understanding Valgrind's output
我正在为大学写一个项目。一切都完成了,通过了所有测试,运行良好,但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
信任Valgrind。它几乎总是显示出存在的问题。
以下是您阅读信息的方式:
RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)
试图从不应该读取的内存位置读取,因为它不属于您的应用程序(未分配、非堆栈等)。
这个无效地址刚好超出了RegPoly
复制构造函数中使用的new运算符分配的空间(您可以看到创建该调用的整个回溯)。
我没有看到您的复制构造函数复制gDataIterator
。
问题在RegPoly.cpp:59上,而不是在DemoPol.cpp:50中。
声明您已经分配了16个字节,但它正在尝试读取超出您分配的8个字节。
请参见:4.2。Memcheck错误消息的解释
可能您正试图访问一个无效的迭代器。const迭代器只是意味着您不能使用此迭代器来修改容器中的值。
与无效迭代器有关的简要说明。
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 赫尔格林德和atomic_flag
- 删除堆后对瓦尔格林德错误感到困惑
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 瓦尔格林德:注意到但未处理的国际奥克特尔;是否有必要处理以及如何找到?
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- 瓦尔格林德错误来自 boost::asio
- 瓦尔格林德可能丢失了,仍然可以到达并被瓦尔格林德检测到并被抑制
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 瓦尔格林德"不支持arch_prctl选项"
- 解释瓦尔格林德输出(可能的误报?
- 了解瓦尔格林德的输出
- 瓦尔格林德挂着没有输出
- 集成瓦尔格林德输出以找出数据竞赛的位置
- 解释瓦尔格林德的跟踪-malloc输出