内存泄漏(Valgrind报告)(c++)
Memory leaks (Valgrind report)(C++)
这是我的简单代码:
#include <cstdlib>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <map>
using namespace std;
class GeneralMatrix {
protected:
int width;
int height;
public:
//Stores values of matrix size
GeneralMatrix(int nr, int nc) {
height = nr;
width = nc;
}
};
class RegularMatrix : public GeneralMatrix {
protected:
vector<double>data;
public:
//Constructor
RegularMatrix(int nr, int nc, const vector<double>& nums) : GeneralMatrix(nr, nc) {
data = nums;
}
};
int main(int argc, char** argv) {
double mm3[] = {10, 2, 3, 0, 0, 8, 0, 4, 2, 2, 6, 0, 0, 0, 0, 5};
vector<double>k;
for (int i = 0; i < 16; i++) {
k.push_back(mm3[i]);
}
GeneralMatrix *d = new RegularMatrix(4, 4, k);
delete d;
return 0;
}
正如你所看到的,我试图调用构造函数,并给它的数字向量存储在类内部向量。一切正常,但Valgrind不知何故发现内存泄漏。
==4903== in use at exit: 128 bytes in 1 blocks
==4903== total heap usage: 7 allocs, 6 frees, 396 bytes allocated
==4903==
==4903== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4903== at 0x4029F34: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4903== by 0x8049679: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (new_allocator.h:104)
==4903== by 0x8049347: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/ubuntu/Desktop/a.out)
==4903== by 0x8048E50: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __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:1138)
==4903== by 0x8048AD5: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (vector.tcc:188)
==4903== by 0x80489C0: RegularMatrix::RegularMatrix(int, int, std::vector<double, std::allocator<double> > const&) (mem2.cpp:37)
==4903== by 0x80488AE: main (mem2.cpp:55)
==4903==
==4903== LEAK SUMMARY:
==4903== definitely lost: 128 bytes in 1 blocks
==4903== indirectly lost: 0 bytes in 0 blocks
==4903== possibly lost: 0 bytes in 0 blocks
==4903== still reachable: 0 bytes in 0 blocks
==4903== suppressed: 0 bytes in 0 blocks
谁能告诉我我做错了什么?谢谢你。编辑:我发布了错误的代码没有删除(现在修复),问题是在使用向量。
您需要将GeneralMatrix的析构函数设置为虚的,否则您将无法通过基指针正确删除派生对象。
不要混淆指针&引用和动态分配。您可以很好地使用指向静态或自动对象的指针或引用。"多态性只适用于指针和引用"与对象的生命周期无关,而是处理它的方式。参见示例:http://ideone.com/qmywMk
#include <iostream>
struct Base {
virtual void sayHi() {
std::cout << "Hi from Base!n";
}
virtual ~Base() {}
};
struct Derived : Base {
virtual void sayHi() {
std::cout << "Hi from Derived!n";
}
};
void sayHelloToMyValue(Base o) {
o.sayHi();
}
void sayHelloToMyReference(Base &o) {
o.sayHi();
}
void sayHelloToMyPointer(Base *o) {
o->sayHi();
}
int main(int, char**) {
Derived d; // No dynamic allocation!
Base &base = d;
sayHelloToMyValue(base);
sayHelloToMyReference(base);
sayHelloToMyPointer(&base);
return 0;
}
第一次调用按值传递base
(copy),因此失去了它的派生类型。
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 使用std::source_location报告错误的最佳实践
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 当用户超过按钮点击限制时报告
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么cudaMemGetInfo报告设备内存总量的变化
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 内存清理程序报告全局对象构造中未初始化值的使用
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 在 Linux 中使用 ioctl() 获取隐藏功能报告时,零字节消失
- Valgrind 在 std::string::swap 中报告 SIGILL
- DRD 报告"conflicting load" std::mutex::lock 上的错误
- 柯南,CMake.test()生成XML报告
- 如何构建一个异常类来报告C++中的哪些文件和行号?
- Valgrind 在 QThread::start() 上报告内存泄漏
- 我是否访问了已释放的内存,或者在这种情况下DrMemory报告不正确?
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 为什么 valgrind 报告两个内存分配,而我的代码只请求一个?
- boost::p rogram_options 在指定意外的位置参数时不报告任何错误