본문 바로가기
C++

ConvertBSTRToString 함수 메모리 누수 문제

by leo21c 2023. 9. 14.
SMALL

BSTR 값을 Char*로 변경을 하기 위해서 ConvertBSTRToString 함수를 사용했는데 메모리 누수가 발생이 되었다.

ConvertBSTRToString 함수를 사용해서 리턴되는 Char*을 삭제를 해야 한다.


VARIANT vtProp;
//-----------------------------------------------------------------------
VariantInit(&vtProp);
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
if (hr == S_OK)
{
	char* pszChar = _com_util::ConvertBSTRToString(vtProp.bstrVal);
	_temp = CString(pszChar);
	delete[] pszChar;
	TRACE(_T("Name : %s\n"), _temp);
}
VariantClear(&vtProp);

위와 같이 리턴 된 값을 사용하고 delete[]를 하면 누를 방지할 수 있다.

<참> https://www.codeproject.com/Articles/4829/Guide-to-BSTR-and-C-String-Conversions

 

Guide to BSTR and C String Conversions

An article on converting to/from C strings and various VB BSTR string types

www.codeproject.com

 

LIST