Who owns returned BSTR?

2019-02-13 10:55发布

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?

标签: com bstr
2条回答
霸刀☆藐视天下
2楼-- · 2019-02-13 11:15

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-13 11:26

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?

查看更多
登录 后发表回答