1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/research/gperftools/heap-checker/heap_checker.cc

70 lines
1.5 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 Winlin
//
// SPDX-License-Identifier: MIT
//
2014-03-06 08:23:35 +00:00
/**
2014-03-07 01:52:59 +00:00
@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html
config srs with gperf(to make gperftools):
./configure --with-gperf --jobs=3
2014-03-06 08:23:35 +00:00
set the pprof path if not set:
export PPROF_PATH=`pwd`/../../../objs/pprof
to check mem leak:
make && env HEAPCHECK=normal ./heap_checker
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
void explicit_leak_imp() {
printf("func leak: do something...\n");
for (int i = 0; i < 1024; ++i) {
char* p = new char[1024];
}
printf("func leak: memory leaked\n");
}
void explicit_leak() {
explicit_leak_imp();
}
char* pglobal = NULL;
void global_leak_imp() {
printf("global leak: do something...\n");
for (int i = 0; i < 1024; ++i) {
pglobal = new char[189];
}
printf("global leak: memory leaked\n");
}
void global_leak() {
global_leak_imp();
}
2014-03-07 01:52:59 +00:00
bool loop = true;
2014-03-06 08:23:35 +00:00
void handler(int sig) {
2014-03-07 01:52:59 +00:00
// we must use signal to notice the main thread to exit normally.
if (sig == SIGINT) {
loop = false;
}
2014-03-06 08:23:35 +00:00
}
int main(int argc, char** argv) {
signal(SIGINT, handler);
global_leak();
printf("press CTRL+C if you want to abort the program.\n");
sleep(3);
2014-03-07 01:52:59 +00:00
if (!loop) {
return 0;
}
2014-03-06 08:23:35 +00:00
explicit_leak();
printf("press CTRL+C if you want to abort the program.\n");
sleep(3);
2014-03-07 01:52:59 +00:00
if (!loop) {
return 0;
}
2014-03-06 08:23:35 +00:00
return 0;
}