【发布时间】:2022-12-01 14:10:22
【问题描述】:
I am analyzing some piece of c++ COM code. I'm trying to find a leak and I found suspicious code in implementation of IPropertyBag2::GetPropertyInfo.
When pPropBag[i].pstrName member is filled some bytes are allocated.
pPropBag[i].pstrName = lstrcpyW(ATL::AtlCoTaskMemCAlloc(name.size() + 1, sizeof(WCHAR)), name.c_str());
I think this code is strange. And I can't find deallocation of bytes for name in this project. May it be cleared from caller of COM interface or automatically cleaned by COM?
I now that is better to use StringCchCopy instead of lstrcpyW
Thank you!
【问题讨论】:
-
See GetPropertyInfo method - Remarks -"When you implement this method, use CoTaskMemAlloc to allocate memory for the pstrName member of pPropBag." ..."When you call this method, use CoTaskMemFree to free the pstrName member of pPropBag"So this memory needs call site (client) clean-up).
-
o! Thank you, I missunderstood this remark