WRL WeakRef
So, you have two objects that are related. A mother gorilla and a baby gorilla. You want the mother gorilla can reference the baby gorilla, and vice versa, so you add a reference to the baby from the mother and another reference to the mother from the baby.
class MotherGorilla : public RuntimeClass<FtmBase> { private: ComPtr<BabyGorilla> baby; }; class BabyGorilla : public RuntimeClass<FtmBase> { private: ComPtr<MotherGorilla> mother; };
Guess what? In COM, where the objects are freed until the reference counter hits zero, the mom's counter will never be zero because the baby has a reference to the mom, and the baby's counter will never be zero because the mom holds a reference to the baby. Possible memory leak.
Fix? Make one of the references a weak reference.
class MotherGorilla : public RuntimeClass<FtmBase> { private: WeakRef baby; }; // BabyGorilla class requires a UUID, otherwise WeakRef::CopyTo() // fails with "error C2787: 'BabyGorilla' : no GUID has been // associated with this object" [uuid("7c8ab438-a275-467f-8bdd-7e556e0016f4")] class BabyGorilla : public RuntimeClass<FtmBase> { private: ComPtr<MotherGorilla> mother; };
And each time the mother wants to interact with the baby, she will need to get a hard reference to the baby by calling WeakRef::As or WeakRef::CopyTo.