I need to use different implementations of some of the methods in my class, therefore I use a couple of boost::function
variables to point to correct method.
everything seems working except this variable:
boost::function<void(void)> onExit
which is called in the destructor. Have a look at my simple methods please:
//constructor
Logger::Logger(std::string id_){
/*...other stuff...*/
//decision point to use which implementation
initDef();
}
//Default Implementation initializer
void Logger::initDef()
{
/*...other stuff...*/
onExit = boost::bind(&Logger::onExitDef,this);
if(onExit.empty()){
std::cout << "onExit is Empty in initialization" << std::endl;
}
}
//destructor
Logger::~Logger(){
if(onExit.empty()){
std::cout << "onExit is Empty in destructor" << std::endl;
}
onExit();//error in here
}
a breakpoint inside initDef()
shows the following after initializing onExit
. As you can see the vtable
is not empty :
Details:{<boost::function0<void>> = {<boost::function_base> = {vtable = 0x1469681 <void boost::function0<void>::assign_to<boost::_bi::bind_t<void, boost::_mfi::mf0<void, Logger>, boost::_bi::list1<boost::_bi::value<Logger*> > > >(boost::_bi::bind_t<void, boost::_mfi::mf0<void, Logger>,
boost::_bi::list1<boost::_bi::value<Logger*> > >)::stored_vtable+1>, functor = {obj_ptr = 0xd32806 <Logger::onExitDef()>, type = {type = 0xd32806 <Logger::onExitDef()>, const_qualified = false, volatile_qualified = false}, func_ptr = 0xd32806 <Logger::onExitDef()>, bound_memfunc_ptr = {memfunc_ptr = (void (boost::detail::function::X::*)(boost::detail::function::X * const, int)) 0xd32806 <Logger::onExitDef()>, obj_ptr = 0x45903200},
obj_ref = {obj_ptr = 0xd32806 <Logger::onExitDef()>, is_const_qualified = false, is_volatile_qualified = false}, data = 6 '\006'}}, static args = <optimized out>, static arity = <optimized out>}, <No data fields>}
but in the destructor, onExit.empty()
holds true(vtable
is empty/null) :
Details:{<boost::function0<void>> = {<boost::function_base> = {vtable = 0x0, functor = {obj_ptr = 0x0, type = {type = 0x0, const_qualified = false, volatile_qualified = false},
func_ptr = 0x0, bound_memfunc_ptr = {memfunc_ptr = NULL, obj_ptr = 0x0}, obj_ref = {obj_ptr = 0x0, is_const_qualified = false, is_volatile_qualified = false}, data = 0 '\0'}}, static args = <optimized out>, static arity = <optimized out>}, <No data fields>}
and naturally, the obvious error is:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_function_call> >'
what(): call to empty boost::function
I will appreciate you you share your thoughts with me: how does this happen and how do you think I can solve this issue?
thanks