Skip to content

Commit 1157972

Browse files
committed
Repair propably memory leaks
1 parent 34f38f6 commit 1157972

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

analyzer.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ static unsigned long average_cpu_usage(struct proc_stat prev,
2121
}
2222

2323
void *analyzer(void *arg) {
24-
struct proc_stat *prev = malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
24+
struct proc_stat *prev =
25+
malloc((unsigned long)g_nproc * sizeof(struct proc_stat));
26+
if (prev == NULL) {
27+
return NULL;
28+
}
2529
unsigned long *avg = malloc((unsigned long)g_nproc * sizeof(unsigned long));
30+
if (avg == NULL) {
31+
free(prev);
32+
return NULL;
33+
}
2634
unsigned long *bufforAvg;
2735
struct proc_stat *stats = NULL;
2836
pthread_cleanup_push(free, prev);

reader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "reader.h"
2+
#include <pthread.h>
23

34
static int get_proc_stats(struct proc_stat *stats) {
45
FILE *file = fopen("/proc/stat", "r");

0 commit comments

Comments
 (0)