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 :-(