在我的追求找到在C++代码一个特别持久的内存泄漏,我决心写所有分配到一个日志文件的格式如下:删除配对在文件
<alloc|free> <address> <size> <UNIQUE-ID> <file> <line number>
这给我,例如:
alloc8 60 405766 file1.cpp (3572)
free 232128 60 405766
alloc8 60 405767 file1.cpp (3572)
free 232128 60 405767
alloc 7a3620 12516 405768 file2.cpp (11435)
free 7a3640 12516 405768
alloc 2306c8 256 405769 file3.cpp (3646)
alloc 746160 6144 405770 file3.cpp (20462)
alloc 6f3528 2048 405771 file4.h (153)
alloc 6aca50 128 405772 file4.h (153)
alloc 632ec8 128 405773 file4.h (153)
alloc 732ff0 128 405774 file4.h (153)
free 746180 6144 405770
free 632ee8 128 405773
alloc 6a7610 2972 405778 this_alloc_has_no_counterpart.cpp (123)
free 6aca70 128 405772
free 733010 128 405774
free 6f3548 2048 405771
alloc 6a7610 2972 405775 file3.cpp (18043)
alloc 7a3620 12316 405776 file5.cpp (474)
alloc 631e00 256 405777 file3.cpp (18059)
free 7a3640 12316 405776
free 6a7630 2972 405775
free 631e20 256 405777
free 2306e8 256 405769
我想匹配每一个alloc
到free
和L檐口只有alloc
s没有free
副本,例如,分配号码405778
。
我能想出的是下面的shell脚本:
#!/bin/sh
grep "^alloc" test.txt | while read line
do
alloc_nr=`echo $line | awk '{ print $4 }'` # arg4 = allocation number
echo "Processing $alloc_nr"
sed -i "/ ${alloc_nr}/{//d}" test.txt
done
正如你可能已经猜到了,这是刻意慢上约144000 alloc
个25MB的文件(即每秒2个循环)。 ,因为我使用sed
的方式非常低效。
如果有人能够在正确的方向上推动如何在不花费三个小时的情况下实现这一目标,我们将非常感激。
最后缺少'}',但非常好,谢谢! – Joost