Who owns returned BSTR?

前提是你 提交于 2019-11-30 04:23:04

问题


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?


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/872794/who-owns-returned-bstr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!