Thread: STL map problem

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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?

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I'm not using a dll. This is on os x.

    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>
      }
    }
    For some reason, the string's address changes after the callback. How can that happen?

    Thanks.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    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_;
    };

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I couldn't get your example to compile as is, since Item did not have a zero-argument constructor. I changed Controller to this:
    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;
    };
    and I got "10 10" as output.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Hey,
    Thanks for your help.
    It turned out there was some stray line of code somewhere that was causing the problem.
    Got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me figure out how to use STL map containers
    By Bozebo in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2011, 09:48 PM
  2. STL Map Problem with user defined Class
    By noobcpp in forum C++ Programming
    Replies: 21
    Last Post: 07-21-2008, 12:02 PM
  3. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  4. map problem
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2006, 06:06 PM
  5. STL map with D3DXVECTOR2 as key help.
    By Pazu in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2006, 11:15 AM