PassengerStatistics/Tools/LeakTracer/main.cpp

44 lines
1.0 KiB
C++
Raw Normal View History

2024-03-25 00:09:33 +08:00
#include "BoostLog.h"
2024-03-23 11:57:23 +08:00
#include "LeakTracer.h"
#include "MemoryAllocationStackTracer.h"
#include <boost/stacktrace.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test() {
std::cout << boost::stacktrace::stacktrace();
malloc(20);
}
int make_memory_leak(void) {
printf("%s:%d, 00!\n", __FILE__, __LINE__);
int *ptr = (int *)malloc(sizeof(int)); // 分配内存块
*ptr = 0; // 构造内存泄露
printf("%s:%d, ptr= %p, *ptr= %d!\n", __FILE__, __LINE__, ptr, *ptr);
char *p_new = new char[8 * 1024 * 1024];
(void)memset(p_new, 0, 8 * 1024 * 1024);
*p_new = 0;
printf("%s:%d, p_new= %p, *p_new= %d!\n", __FILE__, __LINE__, p_new, *p_new);
return 0;
}
class Test {
int a;
};
int main(int argc, char const *argv[]) {
2024-03-25 00:09:33 +08:00
LOG(info) << "hello world.";
MemoryAllocationStackTracer::instance()->start();
2024-03-23 11:57:23 +08:00
auto a = new Test();
2024-03-25 00:09:33 +08:00
make_memory_leak();
MemoryAllocationStackTracer::instance()->dump();
2024-03-23 11:57:23 +08:00
return 0;
2024-03-25 00:09:33 +08:00
}