hwinfo icon indicating copy to clipboard operation
hwinfo copied to clipboard

memory leak

Open kangzehao opened this issue 1 year ago • 4 comments

Hello, in your code, I found two places that easily lead to memory leaks; 1. I noticed that the memory space and resources of wmi.enumerator were not released before each query was run. This can result in a new WMI enumerator object being created each time the query is run, without the old object being freed, resulting in a memory leak. 2. When calling the hr = obj->Get(L" ", 0, &vt_prop, nullptr, nullptr) function multiple times, a new vt_prop object will be created each time it is run. You only use VariantClear(&vt_prop) the last time ;In fact, this will only release one of the vt_prop objects. The correct approach should be to call obj->Get() every time, followed by VariantClear(). 截图2

截图

kangzehao avatar Jan 22 '24 06:01 kangzehao

Hi @Six0Ne, thank you for reporting your concerns. I will evaluate your points.

Concerning your second point: I am not a windows expert at all, so I may be wrong but calling hr = obj->Get(L" ", 0, &vt_prop, nullptr, nullptr) does not look like it is creating a vt_prop. We pass a memory address as argument and the function will use this address and overwrite existing data. We only need to free the address once, don't we? Or is obj->Get(...) allocating additional memory that is not freed on destruction?

lfreist avatar Jan 22 '24 18:01 lfreist

Hello, I mean no harm; Regarding the second point, the theory is the same as what you said, we pass a memory address as a parameter, and the function will use that address and overwrite the existing data. We only need to release the address once; but in fact, doing so will indeed cause the memory to increase abnormally. As a simple test, put auto cpus = hwinfo::getAllCPUs(); into while(1) and run, we will find The memory of the program will grow abnormally;

kangzehao avatar Jan 23 '24 02:01 kangzehao

Well, doesn't look that nice indeed 😅

Feel free to submit a pull request. Else, I will try to approach the issue in the next few days. Thanks for the report!

lfreist avatar Jan 24 '24 05:01 lfreist

Okay, please update it.

kangzehao avatar Jan 25 '24 03:01 kangzehao