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

Significant memory leak in win32pdh.GetFormattedCounterArray() #2013

Closed
iglendd opened this issue Feb 8, 2023 · 2 comments
Closed

Significant memory leak in win32pdh.GetFormattedCounterArray() #2013

iglendd opened this issue Feb 8, 2023 · 2 comments

Comments

@iglendd
Copy link

iglendd commented Feb 8, 2023

static PyObject *PyPdhGetFormattedCounterArray(PyObject *self, PyObject *args)

A script similar to the one below will leak a gigabyte memory per minute (due to tight loop of course). The leak appears to be caused by the fact of not releasing the key after it has been added to the dictionary which adds its own reference.

key = PyWinObject_FromTCHAR(pItems[i].szName);

It is worth noting that the function has an unrelated logical flaw - it currently cannot handle duplicate instance names (https://learn.microsoft.com/en-us/windows/win32/perfctrs/handling-duplicate-instance-names). Not sure how it should be addressed. Either creating a duplicate function (waste) or enhancing the function to have a value in the dictionary as formatted value or a list, when there is a duplicate instance (but it may break existing usage). On the other hand, due to very significant memory leak without prior reporting perhaps the function is not actively used and can be modified.

import win32pdh

# will generate path "\LogicalDisk(*)\Free Megabytes"
path = win32pdh.MakeCounterPath( (None, "LogicalDisk", "*", None, 0, "Free Megabytes")) 
win32pdh.ValidatePath(path)                          

hq = win32pdh.OpenQuery()                                       
hc = win32pdh.AddEnglishCounter(hq, path)                       

try:
   win32pdh.CollectQueryData(hq)                               
except Exception:
   pass

win32pdh.CollectQueryData(hq)                                  

while True:
   vals = win32pdh.GetFormattedCounterArray(hc, win32pdh.PDH_FMT_DOUBLE)
@mhammond
Copy link
Owner

Thanks - fixed in 1962631

@ofek
Copy link
Contributor

ofek commented Mar 25, 2023

Awesome! When might this be released?

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

3 participants