Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pefile.PE leaks memory #297

Open
baxitaurus opened this issue Sep 30, 2020 · 0 comments
Open

pefile.PE leaks memory #297

baxitaurus opened this issue Sep 30, 2020 · 0 comments

Comments

@baxitaurus
Copy link

baxitaurus commented Sep 30, 2020

PROBLEM

After the close method of a pefile.PE instance has been called, the garbage collector can't destroy the object due to a lot of reference cycles. This obviously causes memory leaks in long-time running processes.

HOW TO REPRODUCE

Python version 2.7.17
pefile version: 2018.8.8

Launch an htop parallely to the below code and look for RAM usage increasing more and more

import pefile

while True:
    pe = pefile.PE(<filepath>)
    pe.close()

or simply run the below code:

import pefile

class Test(pefile.PE):
    def __del__(self):
        print("Destroyed")

pe = Test(<filepath>)
pe.close()
pe = None

...won't print at all.

WORKAROUND (ugly)

Create a wrapper around the PE class:

class MyPE(pefile.PE):
    def close_recursive(obj, target):
        if isinstance(obj, list):
            for val in obj:
                close_recursive(val, target)
        elif isinstance(obj, object):
            try:
                dictobj = obj.__dict__
                if target in dictobj:
                    setattr(obj, target, None)
                for attr, val in dictobj.items():
                    close_recursive(val, target)
            except:
                pass

    def close(self):
        for attr, val in self.__dict__.items():
            close_recursive(val, "pe")
        super(MyPE, self).close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant