Understanding the lifecycle of winrt::hstring with ABI handling

Shyam Butani 285 Reputation points
2024-11-14T11:10:33.95+00:00

Hello,

I'm trying to understand the lifecycle of winrt::hstring when working with the ABI and managing reference counts.

Specifically, I want to store an winrt::hstring in a void* pointer, and to do so, I'm using the copy_to_abi API. This increases the reference count of the internal string, preventing it from being destroyed while the void* pointer is in use.

Once I'm done with the void* pointer, I need to safely decrement the reference count to destroy the winrt::hstring. However, I couldn't find any direct method to decrement the reference count. So, here's what I am doing:

void Func1()
{
    void* ptr = nullptr;

    { // Start Block1

        winrt::hstring myString = L"Hello, WinRT!";

        copy_to_abi(myString, ptr); // Increase the ref count

    } // End Block1

    // ... Consume ptr

    winrt::hstring myString2(ptr, winrt::take_ownership_from_abi);

} // Decrease the ref count since myString2 got out-of-scope

Explanation:

  • Inside Block 1, a winrt::hstring (myString) is created and its pointer is stored in ptr using copy_to_abi, which increases the reference count.
  • After Block 1 ends, I use take_ownership_from_abi to create a new winrt::hstring (myString2) from ptr, which takes ownership of the string and manages the reference count.
  • When myString2 goes out of scope, the reference count is decremented, and the winrt::hstring should be destroyed if no other references exist.

My question is: Is this approach correct for managing the lifecycle of winrt::hstring in this case? Will this work as expected, ensuring that the string is properly destroyed when no longer needed?

Thanks!

Universal Windows Platform (UWP)
{count} vote

Accepted answer
  1. Junjie Zhu - MSFT 19,301 Reputation points Microsoft Vendor
    2024-11-18T03:48:01.8366667+00:00

    Hello @Shyam Butani ,

    Yes, it is valid to put the pointer into a winrt::hstring via “take ownership from abi” and then allowing the winrt::hstring to destruct.

    You can use detach_abi to avoid bumping the reference count:

    int main()
    {
        hstring str = L"hello world";
        void* ptr = detach_abi(str);
        hstring str2(ptr, take_ownership_from_abi);
        assert(str == L"");
        assert(str2 == L"hello world");
    }
    
    

    Another valid way is to call WindowsDeleteString((HSTRING)ptr), which is the OS’s ABI method for managing the lifetime ABI HSTRINGs.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.