I do not know what platform you are on but is this callback coming from a DLL? If so is your module and the DLL using the same CRT and STL version?
I do not know what platform you are on but is this callback coming from a DLL? If so is your module and the DLL using the same CRT and STL version?
I'm not using a dll. This is on os x.
For some reason, the string's address changes after the callback. How can that happen?Code:// WITHOUT CALLBACK EVENT $1 = { static npos = 4294967295, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider: _M_p = 0x1b0d05c "a" } } // WITH CALLBACK EVENT $1 = { static npos = 4294967295, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider: _M_p = 0x342174a9 <Address 0x342174a9 out of bounds> } }
Thanks.
Hi,
Here is what I'm using to do my callbacks. I've determined that (for a reason unknown to me) that when test() is called through the callback, it gives the wrong value. Can anyone see why this might be? It seems like my callback isn't properly referencing the instance of Controller or something?
Thanks for the help!
Code:class Controller { public: Controller() { w = 10; // This prints the proper value for w... test(); myItem = Item(new CallBack<Controller>(this, &Controller::test)); // This prints an incorrect value for w... myItem.callMe(); } void test() { cout << w << "\n"; } int w; Item myItem; }; class Item { public: Item(BaseCallBack* CallbackObject) { callbackObject = CallbackObject; } void callMe() { callbackObject->callback(); } BaseCallBack* callbackObject; };Code:class BaseCallBack { public: virtual void callback() = 0; }; template <class Class> class CallBack : public BaseCallBack { public: typedef void (Class::*Method)(); CallBack(Class* class_instance, Method method) : class_instance_(class_instance), method_(method) {} void callback() { return (class_instance_->*method_)(); } private: Class *class_instance_; Method method_; };
I couldn't get your example to compile as is, since Item did not have a zero-argument constructor. I changed Controller to this:
and I got "10 10" as output.Code:class Controller { public: Controller() : myItem(new CallBack<Controller>(this, &Controller::test)) { w = 10; // This prints the proper value for w... test(); //myItem = Item(new CallBack<Controller>(this, &Controller::test)); // This prints an incorrect value for w... myItem.callMe(); } void test() { cout << w << "\n"; } int w; Item myItem; };
Hey,
Thanks for your help.
It turned out there was some stray line of code somewhere that was causing the problem.
Got it now.