这小东西一直听说,最近忙完暑期实习,研究一下,先研究了下检测内存泄漏,后面研究一下他的Cachegrind,毕竟有可能发生伪共享事情。
这个内存泄漏是真的不错,泄漏了几块内存都会告诉你,值的注意的地方是,需要-g编译出来的可执行文件,valgrind才能准确指出那里是那行内存泄漏了看到这篇博客才知道要带-g
带-g检测的后面都会跟着行号
不带-g就不会显示了
关于still reachable的理解,刚学会这个valgrind就跑去测试了一下之前写的Pine网络库,毕竟新项目用的都是智能指针想这不会有内存泄露的问题,跑的时候发现有仍然可见的内存。我初步怀疑就是因为网络库是个Loop,一直没有退出,而在外面堆上有东西造成的。
如下代码
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
void fun(){
// int* a = new int(2);
// int* a2 = new int(2);
// int* a3 = new int(2);
// delete a2;
}
int main(){
fun();
vector<shared_ptr<int>> nums;
shared_ptr<int> sp = make_shared<int>(2);
nums.emplace_back(sp);
int* a = new int(2);
while(1){
cout << sp.use_count() << endl;
}
return 0;
}
确实是这个样子的
possibly lost我的项目是因为开了几根线程造成的,因为线程都是死循环,并没有退出,测试代码如下
#include <iostream>
#include <memory>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <thread>
using namespace std;
void fun(){
while(1){}
}
int main(){
vector<thread> threadPool;
thread t(fun);
threadPool.emplace_back(std::move(t));
threadPool[0].join();
return 0;
}