lfreist / hwinfo

cross platform C++ library for hardware information (CPU, RAM, GPU, ...)
MIT License
433 stars 74 forks source link

memory leak #76

Open Six0Ne opened 7 months ago

Six0Ne commented 7 months ago

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

截图

lfreist commented 7 months ago

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?

Six0Ne commented 7 months ago

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;

lfreist commented 7 months ago

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!

Six0Ne commented 7 months ago

Okay, please update it.