say we have:
class A
{
void Func(std::function<void(void)> handler);
}
class B
{
A myObj;
B()
{
myObj.Func([this]()
{
//whatever
});
}
}
That makes myObj (of type A) have a reference to lambda object, lambda object has reference to B object (through captured 'this'), and B object (captured 'this') has reference to myObj (of type A), which means a cyclic dependency has occurred, right?
I know the solution lies in the use of std::weak_ptr somehow, but I am having trouble understanding how to properly use them to solve this case. Could anyone help with a solution for this example?
Thanks!