locked
Reference counting and detecting cycles with WinRT types.

    Question

  • The WinRT types are used via reference counting smart pointers.  For memory management, are these types sophisticated enough to detect cycles or will this cause memory leaks?

    For example, if I have two WinRT types A and B.

    {

    A^ a = ref new A();

    B^ b = ref new B();

    a->RegisterB(b);

    b->RegisterA(a);

    }

    where B::RegisterA(A^) and A::RegisterB(B^) simply set and hold a reference to the argument, will these two references be deleted if there are no other references to them other than each other or do I need to account for this?

    Thursday, May 10, 2012 5:06 PM

Answers

  • Hello Mbradber,

    "See Tobias' talk on Modern ISO C++ which answers this for std::shared_ptr and std::weak_ptr, and why such circular references are pretty much always a wrong design in the first place (example: how often is it ever semantically correct for A to own B and control its lifetime, and B to own A and control its lifetime?). It's exactly the same issue: In WinRT, the analog for shared_ptr is ^, and for weak_ptr is IWeakReference."

    Taken from the comments on this page:

    http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-532T

    Writing modern C++ code: how C++ has evolved over the years
    http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-835T

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, May 11, 2012 12:48 AM
    Moderator