NASM 2.10.06
[nasm.git] / misc / findleak.txt
blob8a1cfdc168e3266d9d228553755c642044078cb4
1 Subject: [nasm-devel] tool to help find memory leaks
2 Date: Fri, 02 Nov 2001 22:08:01 -0500
3 From: Ed Beroset <beroset@mindspring.com>
4 Reply-To: nasm-devel@yahoogroups.com
5 To: nasm-devel@yahoogroups.com
7 Here's a little Perl script I wrote a while ago to help track down memory
8 leaks in nasm.  First, compile nasm with LOGALLOC defined (see
9 nasmlib.c).  That creates a log file of all allocs and frees.  This Perl
10 script reads that file and tells you which source code lines caused a leak
11 (or a free of unallocated memory).  There are many leaks, almost all of
12 them in the preprocessor.
14 -+--- findleak.pl begins
15 #!/usr/bin/perl
16 my %mem = {};
17 my %alloc = {};
18 while(<>)
20         if (/realloc\((0x[0-9a-f]+).*\).*returns \((0x[0-9a-f]+)/)
21         {
22                 $mem{$1}--;
23                 if ($mem{$1} != 0) {
24                         print "free before alloc! $_";
25                 }
26                 if ($mem{$2} != 0) {
27                         print "memory leak! $_";
28                 }
29                 $mem{$2}++;
30                 $alloc{$2} = $_;
31         }
32         elsif (/free\((0x[0-9a-f]+)/)
33         {
34                 $mem{$1}--;
35                 if ($mem{$1} != 0) {
36                         print "free before alloc! $_";
37                 }
38         }
39         elsif (m/returns (0x[0-9a-f]+)/)
40         {
41                 if ($mem{$1} != 0) {
42                         print "memory leak! $_";
43                 }
44                 $mem{$1}++;
45                 $alloc{$1} = $_;
46         }
48 foreach $goo (sort keys %mem)
50         if ($mem{$goo})
51         {
52                 print "$mem{$goo} $alloc{$goo}";
53         }
55 -+--- findleak.pl ends
59 Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/