cloudbase / PyMI

A blazing fast replacement for the Python WMI module
Apache License 2.0
21 stars 14 forks source link

Listener crash #39

Closed petrutlucian94 closed 5 years ago

petrutlucian94 commented 5 years ago

Our WMI event listener can only be used as a daemon at the moment. If it gets deallocated, MI crashes with an memory access violation error.

We have to make sure that the event subscription is properly closed before cleaning up the objects that might be used by the subscription callbacks.

Fixes: #36

ader1990 commented 5 years ago

I verified this PR with the following code:

import wmi
c = wmi.WMI()
wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'"
process_watcher = c.Win32_Process.watch_for(wql)
for i in range(20):
    try:
        # milliseconds
        new_process = process_watcher(2000)
        print(new_process.Caption)
    except wmi.x_wmi_timed_out:
        pass

When running without the patches in this PR, the exit code of the python script is: -1073741819

When running with the patches in this PR, the exit code is always 0.