Go Back   EQEmulator Home > EQEmulator Forums > Archives > OpenEQ > OpenEQ::Development

OpenEQ::Development Development discussion for OpenEQ. Do not post for support.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-30-2004, 11:43 AM
daeken_bb
Discordant
 
Join Date: Mar 2003
Location: Chambersburg, PA
Posts: 469
Default 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)
__________________
Keep me unemployed and working on OpenEQ, PM me about donating

Check out my deviantART page at http://daeken.deviantart.com/
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 12:31 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3