c++bstrclr-hosting

Do I need to free BSTRs populated by _Type::get_FullName


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?


Solution

  • 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.