Skip to content

Commit 42db62c

Browse files
committed
test/on_exit: use static variables for on_exit hooks
before this change, we allocate memory chunks using malloc(), but we never free them. and LeakSanitizer points this out ``` Direct leak of 4 byte(s) in 1 object(s) allocated from: #0 0x5588bfe532de in malloc (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_on_exit+0xa52de) (BuildId: 7c7a92bf5719592938c5307214bcd9b080bd847f) ceph#1 0x5588bfe911d7 in func_scope() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:33:22 ceph#2 0x5588bfe90804 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:64:3 ceph#3 0x7f23081c1d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 Direct leak of 4 byte(s) in 1 object(s) allocated from: #0 0x5588bfe532de in malloc (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_on_exit+0xa52de) (BuildId: 7c7a92bf5719592938c5307214bcd9b080bd847f) ceph#1 0x5588bfe91160 in func_scope() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:29:22 ceph#2 0x5588bfe90804 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:64:3 ceph#3 0x7f23081c1d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 ``` in this change, instead of allocating the variables using `malloc()`, we keep them in static variables, so that they can be accessed by `OnExitManager` even if it is a static variable. with this change, the memory leak reports for this source file go away. Signed-off-by: Kefu Chai <[email protected]>
1 parent d6c8171 commit 42db62c

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/test/on_exit.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ static void func_scope(void)
2626
{
2727
OnExitManager mgr;
2828

29-
int *inc_1 = (int*)malloc(sizeof(*inc_1));
30-
*inc_1 = 5;
31-
mgr.add_callback(add, inc_1);
29+
static int inc_1 = 5;
30+
mgr.add_callback(add, &inc_1);
3231

33-
int *inc_2 = (int*)malloc(sizeof(*inc_2));
34-
*inc_2 = 3;
35-
mgr.add_callback(add, inc_2);
32+
static int inc_2 = 3;
33+
mgr.add_callback(add, &inc_2);
3634
}
3735

3836
// shared between processes
@@ -84,9 +82,8 @@ int main(int argc, char **argv)
8482
// exits by returning from main. The parent checks the value after the
8583
// child exits via the memory map.
8684
ceph_assert(*shared_val == 0);
87-
int *new_val = (int*)malloc(sizeof(*new_val));
88-
*new_val = MAIN_SCOPE_VAL;
89-
main_scope_mgr.add_callback(main_scope_cb, new_val);
85+
static int new_val = MAIN_SCOPE_VAL;
86+
main_scope_mgr.add_callback(main_scope_cb, &new_val);
9087
return 0;
9188
}
9289

@@ -104,9 +101,8 @@ int main(int argc, char **argv)
104101
// child adds a callback to the static scope callback manager and then
105102
// exits via exit().
106103
ceph_assert(*shared_val == 0);
107-
int *new_val = (int*)malloc(sizeof(*new_val));
108-
*new_val = EXIT_FUNC_VAL;
109-
exit_func_mgr.add_callback(exit_func_cb, new_val);
104+
static int new_val = EXIT_FUNC_VAL;
105+
exit_func_mgr.add_callback(exit_func_cb, &new_val);
110106
call_exit();
111107
ceph_abort();
112108
}

0 commit comments

Comments
 (0)