Reputation: 1838
I have a function like this:
bool Function (BSTR paramter1, BSTR parameter2)
{
// My Code
}
which I am calling like this:
Function(SysAllocString(L"Example1"), SysAllocString(L"Example2"));
My understanding is that what I allocate with SysAllocString
must be freed with SysFreeString
.
BSTR
seems to be a typedef under the hood of typedef OLECHAR *BSTR;
Since this is a pointer, I should be fine as long as I add
SysFreeString(parameter1);
SysFreeString(parameter2);
inside Function
, and don't name the BSTR
s.
Is all of my reasoning here correct, and as long as I add these two lines, I won't memory leak? I still have a lot to learn. Thank you very much for your time.
Upvotes: 1
Views: 436
Reputation: 781380
Yes, you're correct. But this is usually not the appropriate way to design your software. In general, the responsibility for freeing an object goes to the routine that allocated it, or some related routine. You should be able to call Function
with parameters that don't need to be freed right away, or with parameters that are declared locally rather than allocated on the heap.
Upvotes: 4