问题描述
你好,我正在为图形编辑器做一个文件对话模块。
Hello, I was doing a File Dialog module for a graphics editor.
为此,我使用了IFileDialog,包裹周围有一个WRL :: ComPtr<>
For it I used IFileDialog, wrapped around with a WRL::ComPtr<>
std::wstring VFileOpenDialog::GetFilePath()
{
HRESULT hr;
if (SUCCEEDED(pDialog->Show(NULL)))
{
Microsoft::WRL::ComPtr<IShellItem> psiResult;
if (SUCCEEDED(pDialog->GetResult(&psiResult)))
{
PWSTR pszFilePath = NULL;
if (SUCCEEDED(psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath)))
{
return pszFilePath;
}
}
}
return std::wstring();
}
在这段代码之后,它必须将文件名传递给文件流,但在此结束后我看到了大量内存泄漏。
我做了一个完整的测试,用普通的Raw Pointers替换了ComPtrs,但没有任何帮助。如果我不启动这个方法,我的内存使用量是3 Mb,但是当这个方法完成时它会提升到17-20Mb 。
After this chunk of code it has to pass a filename to a file stream, but I saw a large memory leak after this ends.
I did a full testing, replaced ComPtrs with regular Raw Pointers, but nothing is helping. If I don't launch this method my memory usage is at 3 Mb, but when this method finishes it raises up to 17-20Mb.
如何克服这些泄漏并释放内存?
How do I overcome those leaks and free a memory?
推荐答案
此代码泄漏了pszFilePath指向的字符串,因为未调用CoTaskMemFree。
This code is leaking the string pointed to by pszFilePath since CoTaskMemFree is not called.
PWSTR pszFilePath = NULL;
if (SUCCEEDED(psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath)))
{
return pszFilePath;
}
这篇关于IFileDialog吃了17-20 Mb的内存,并没有释放自己。 - &安培; GT;移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!