WMI内存泄漏问题

时间:2017-04-08 20:19:12

标签: c++ qt memory-leaks windows-applications

更改语言时,我遇到了 public Form1() { InitializeComponent(); agetxt.KeyPress += agetxt_KeyPress; } private void agetxt_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar)) { e.Handled = true; } } 内存泄漏问题。我在WMI中查了一下。例如,我的应用程序需要25 MB Task Manager,当更改语言时,它会增长到30 MB和35,40 ......并且永远不会释放它。

RAM

如何解决此问题?提前谢谢。

测试我检查过内存泄漏的屏幕截图:

代码:

//Initialization
IWbemLocator *pLocator = 0;
IWbemServices *pService = 0;
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj = NULL;

while (pEnumerator)
{
      hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

      VARIANT processName;
      pclsObj->Get(L"Name", 0, &processName, 0, 0);
      QString userProcessName;
      userProcessName = QString::fromWCharArray(processName.bstrVal);

      emit testData(userProcessName);
      VariantClear(&processName);
}

//Cleanup
 pService->Release();
 pLocator->Release();
 //pEnumerator->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
 //pclsObj->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null

1 个答案:

答案 0 :(得分:2)

@IInspectable

谢谢。我通过释放资源修复了WMI内存泄漏问题。

<强>代码:

...
            VariantClear(&serviceName);
            VariantClear(&servicePath);
            VariantClear(&serviceID);
            VariantClear(&serviceType);
            VariantClear(&serviceState);
            VariantClear(&serviceStatus);
            VariantClear(&serviceErrorControl);
            VariantClear(&serviceStartMode);
            VariantClear(&serviceWaitHint);
            VariantClear(&serviceExitCode);
            pclsObj->Release();
        }
    }

    // Cleanup
    pService->Release();
    pLocator->Release();
    pEnumerator->Release();
    CoUninitialize();
    emit finished();
    return 0;   // Program successfully completed.
}