0

I have some code that looks like this:

_TypePtr spType = NULL;
. . . // host the CLR and populate that type
{
    BSTR fullName;
    spType->get_FullName(&fullName);
    wprintf(L"Got type %s\n", fullName);
}

Do I need to free that bstr? How do I free it SysFreeString()? If not why?

Justin Dearing
  • 14,270
  • 22
  • 88
  • 161

1 Answers1

1

A BSTR is dynamically allocated by SysAllocString (if I recall the name correctly, check it!).

There is a corresponding deallocation function.

Just read the documentation.


If you're using the Visual C++ compiler, and don't plan on ever porting the code to other compilers, then you can use the "smart BSTR" class bundled with Visual C++. I can't exactly recall the name. But something like _bstr_t. Wait, checking the docs... OK, typing "_bstr" in the index supplied the name, it is _bstr_t as I thought! :-)

With use of the "smart" class it handles deallocation for you.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • That was obvious in retrospect. I obviously need to read [Eric's Complete Guide To BSTR Semantics](http://blogs.msdn.com/b/ericlippert/archive/2003/09/12/52976.aspx) – Justin Dearing Dec 26 '12 at 23:38
  • `bstr_t` and `ATL::CComBSTR` both are adequate for a smart BSTR class mentioned in this answer. – WhozCraig Dec 26 '12 at 23:42