EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   OpenEQ::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=609)
-   -   Memory leak assistant (https://www.eqemulator.org/forums/showthread.php?t=16117)

daeken_bb 09-30-2004 11:43 AM

Memory leak assistant
 
I whipped up a script a few days ago to assist in finding memory leaks. This script will report all instances of malloc() and free() and even tell you where the memory could've been allocated at in the case of free().

Full source is available at http://home.archshadow.com/~daeken/seeker.py and below.

Code:

import re, sys

def main(args):
    malloc = re.compile('([_a-zA-Z0-9\->.\[\]*+/-\^&|<~() ]+)[ \t]*=.*?[^a-zA-Z0-9]malloc[ \t]*\([ \t]*(.*)[ \t]*\)')
    malloci = re.compile('if[ \t]*\([ \t]*([_a-zA-Z0-9\->.\[\]*+/-\^&|<~() ]+)[ \t]*=.*?[^a-zA-Z0-9]malloc[ \t]*\([ \t]*(.*)[ \t]*\)[ \t]*\)')
    free = re.compile('[^_a-zA-Z9-9]?free[ \t]*\([ \t]*(.*?)[ \t]*\)[ \t]*;')

    lines = file(args[0]).readlines()
    allocated = {}
    lc = 1
    for l in lines:
        l = l.strip()
        r = malloci.search(l)
        if not r:
            r = malloc.search(l)
        if r:
            var, count = r.group(1, 2)
            pc, b = 0, ''
            for c in tuple(count):
                if c == '(':
                    pc += 1
                elif c == ')':
                    if pc == 0:
                        break
                    pc -= 1
                b += c
            print 'Malloc: (%s) bytes allocated to `%s\' on line %i.' % (b, var, lc)
            if not var in allocated:
                allocated[var] = []
            allocated[var].append(str(lc))
        else:
            r = free.search(l)
            if r:
                var = r.group(1)
                if not var in allocated:
                    print 'Free: Freed `%s\' (not seen previously in this file) on line %i.' % (var, lc)
                else:
                    print 'Free: Freed `%s\' (previously allocated on line(s)' % var, ', '.join(allocated[var]), ')', 'on line %i.' % lc
        lc += 1

if __name__=='__main__':
    sys.exit(main(sys.argv[1:]))

Enjoy.

Happy Hacking,
Lord Daeken M. BlackBlade
(Cody Brocious)


All times are GMT -4. The time now is 02:03 PM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.