Bind a class member function and a valid class object pointer to boost::function<>fn. What may happen if invoking the fn after the object which the pointer pointed to has been destroyed?
Are there some potential problems that I should be aware of?
Domo code snappet:
 class CTest
 {
    public:
         int demo(void){}
 };
 
 int main()
 {
    boost::function<int(void)> fn;
    {
        CTest ins;
        fn = boost::bind(&CTest::demo, &ins); 
    }
    fn();
}
Edited(https://godbolt.org/z/r8EK1G)
Quoted from the comment of j6t
One way to do that is to pass the object by value, not a pointer to the object. Then a copy of the object would be used during the invocation fn(). I think there is still a problem that the object 'tes' is out of scope.So passing value is not a good method. :
 #include<functional>
 #include<iostream>
 class CTest
 {
    public:
         int demo(void){std::cout << "do better" << std::endl;return 0;}
 };
template <class T=CTest>
std::function<int(void)>  bindWarp(T obj, int (T::*mem_func)(void))
{
    return std::bind(mem_func, obj);
}
 int main()
 {
    std::function<int(void)> fn;
    {
        CTest tes;
        fn = bindWarp(tes, &CTest::demo);  
    }
    fn();  //I think there is still a problem that the object 'tes' is out of scope.So passing value is not a good method.
}
 
    