Errata – RCW issue when returning a BSTR

I hate being proved wrong as much as the next guy, but I owe it to my blog readers to correct a confusion that I’ve put into their minds due to a short sighted blog entry I made several months ago. For those of you who remember, I made a blog entry titled RCW issue when returning a BSTR a while ago. Guess what? Some of the Microsoft folks have commented to me that the information in the entry is incorrect!

Essentially, the interop marshaller always frees memory allocated by unmanaged code during an interop transition. Thus if the interop marshaller creates a System::String from a BSTR, it’ll free the BSTR knowing that the managed code has no access to it (the BSTR). If you manually prototype the functions to use IntPtr types, the marshaller will not attempt to free the memory – so that’s handy if you want to have control over when the memory is cleaned up.

Anyway, what annoys me and makes me feel like kicking myself is that, the bug (the non-existent bug that was conceived in my stupid brain) was too obvious a bug to have gone through any decent beta testing phase. And if it existed, 100s or even 1000s of people would have reported memory leaks in their RCW attempts. To think that I couldn’t logically conclude that the bug was too noticeable to have existed for so long is a sad reflection on my logical thinking powers.

It sucks that after so many years of considering myself to be a smart fellow, I have to learn at this ripe old age that I am about as intelligent as a moronic fish 😦

Advertisements

3 thoughts on “Errata – RCW issue when returning a BSTR

  1. I am seeing the same problem: a leaked BSTR shows up in bounds checker under exactly teh same circumstances as your original entry. The comments left me confused. Is this a microsoft bug or an erroneous report by the memory checker?

  2. I also still see BSTR’s being leaked in this situation, shown in BoundsChecker and Memory Validator.

    Any idea ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s