classSolution { public: inttitleToNumber(string s){ int n = s.size(); int res = 0; for (int i = 0; i < n; i++) { int tmp = s[i] - 'A' + 1; res = res * 26 + tmp; } return res; } };
root@top:~# /usr/share/bcc/tools/memleak -a -p $(pidof app) Attaching to pid 10612, Ctrl+C to quit. [13:41:44] Top 10 stacks with outstanding allocations: addr = 7fc8c005f220 size = 8192 addr = 7fc8c0067260 size = 8192 addr = 7fc8c0063240 size = 8192 addr = 7fc8c0065250 size = 8192 addr = 7fc8c0061230 size = 8192 40960 bytes in 5 allocations from stack fibonacci+0x1f [app] child+0x56 [app] start_thread+0xe4 [libpthread-2.28.so]
对child函数进行修正:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void *child(void *arg) { longlong n0 = 0; longlong n1 = 1; longlong *v = NULL; int n = 2; for (n = 2; n > 0; n++) { v = fibonacci(&n0, &n1); n0 = n1; n1 = *v; free(v); printf("%dth => %lld\n", n, *v); sleep(1); } }
再次编译运行代码,并执行memleak工具,可以看到已经修正成功。
1 2 3 4 5
root@top:~# /usr/share/bcc/tools/memleak -a -p $(pidof app) Attaching to pid 10775, Ctrl+C to quit. [13:50:04] Top 10 stacks with outstanding allocations: [13:50:09] Top 10 stacks with outstanding allocations: [13:50:14] Top 10 stacks with outstanding allocations: