Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who owns returned BSTR?

Tags:

com

bstr

Suppose a method from a COM interface returns BSTR value. Am I right in my opinion that I must free it?

The code example at http://msdn.microsoft.com/en-us/library/aa365382(VS.85).aspx does not do that.

Who's wrong?

like image 724
Sergey Skoblikov Avatar asked May 16 '09 16:05

Sergey Skoblikov


2 Answers

The MSDN sample is wrong. The caller frees out and in/out bstrs. If it's in/out you have to pass in null or a valid bstr. If it's out only, it doesn't have to be initialized.

It's not super clear from msdn's com allocation rules, but the client stub allocates the memory on out values so from the caller's point of view the server did. Who else can free it but the caller?

like image 172
Tony Lee Avatar answered Oct 05 '22 04:10

Tony Lee


Tony is correct but I wanted to expand on his answer. The situation is not just limited to a BSTR. The caller is responsible for freeing any memory that is returned via out or in/out parameters.

like image 32
JaredPar Avatar answered Oct 05 '22 04:10

JaredPar