StackUser
StackUser

Reputation: 349

How to clear a memory leak in BSTR

When running my application, it has a memory growth. So I collected UMDH data and it points to the below code,

BSTR bstrTestName = strCurTestName.AllocSysString();
 BSTR bstrTestInstname = strTestInstName.AllocSysString();
HRESULT hr = g_pDLServiceNetInterop->SetCurTestName(bstrTestName, pCurTestData->GetTestType(), bstrTestInstname, pCurTestData->GetTestMemberCount());

where strCurTestName and strTestInstName are TString.

I don't see a call to SysFreeString in this method.

I know that if we pass a BSTR to a client, it will take care of clearing the memory. In the current scenario, is this a leak and I need to call SysFreeString or it is not a leak.

Upvotes: 0

Views: 605

Answers (2)

StackUser
StackUser

Reputation: 349

It is a leak. We need to explicitly clear it. Instead of BSTR, I used CComBSTR and it resolved the leak (yes, calling SysFreeString also resolved the leak).

Upvotes: 2

Nikita Cheblokov
Nikita Cheblokov

Reputation: 1

You can call the SysFreeString(bstrTestName) function to deallocate the string.

Edit: as pointed out by the comment, since you are not passing the string to the client, just like with any allocated memory, it will stay allocated unless you free it with a the SysFreeString function call.

Upvotes: 0

Related Questions