You should not free the line because the caller can potentially reuse the transferred data, and if you are free, there may be an error. The reason is that FreeBSTR
does not use the reference counting mechanism and simply calls SysFreeString , which, by the way, assumes that the line is allocated by one of the Sys(Re)Alloc...
functions, a circumstance that you do not know about in managed code. The example shown here is interesting, imagine the unmanaged code calling you, this is this (from the link earlier):
// shows using the Win32 function // to allocate memory for the string: BSTR bstrStatus = ::SysAllocString(L"Some text"); if (bstrStatus != NULL) { pBrowser->put_StatusText(bstrStatus); // Free the string: ::SysFreeString(bstrStatus); }
and you got put_StatusText(...)
in managed code, we reproduce your situation. As you can see, the caller is responsible for highlighting / releasing the parameter string outside the callee.
source share