Let's say I have the following scenario where I define a callback function m_deleter.
class B {
public:
    B() = default;
    ~B(){
        if (m_deleter) {
            m_deleter();
        }
    }
    std::function<void()> m_deleter = nullptr;
};
class A {
public:
    void createB(B& b) {
        auto func = [this]() {
            this->printMessage();
        };
        b.m_deleter = func;
    }
    void printMessage() {
        std::cout << "Here is the message!" << std::endl;
    }
};
And here is our main function:
int main() {
    B b;
    {
        A a;
        a.createB(b);
    } // a falls out of scope.
}
Here is my confusion. When the stack instance a falls out of scope, is the memory not deallocated? How can the this pointer, used here in the callback: this->printMessage(); still point to a valid object?
When I run the above program, it prints:
Here is the message!
Edit:
Follow up question, is there any way for b to know that a has fallen out of scope and is no longer a valid object, and therefore should not call the callback?
 
     
    