Overview
Features
Download
Documentation
Community
Add-Ons & Services

POCO::delegate and inheritance

Please post support and help requests here.

POCO::delegate and inheritance

Postby dbarzo » 20 Feb 2014, 13:32

Hi, I have a class (named MessageEventHandler) which register a delegate and associates it to an opCode:

Code: Select all
template <class TObj, class TArgs>
ERROR_CODE RegisterMsg(UINT msg_opcode, Poco::Delegate<TObj, TArgs, true> delegate) {
  TEventPtr ev = m_EventsMap[msg_opcode];
  if (ev.isNull()) ev.assign(new TEvent());
  (*ev) += delegate;
  m_EventsMap[msg_opcode] = ev;
  return EC_NO_ERRORS;
};


Where m_EventMap is defined as:

Code: Select all
typedef BasicEvent<TArgs>         TEvent;
typedef SharedPtr<TEvent>         TEventPtr;
typedef std::map<INT, TEventPtr>  TEventMap;

TEventMap       m_EventsMap;


Now, I have a base class which defines a method to be used as delegate:

Code: Select all
class EventsHandler {
public:

  void OnMsgMethod(const void* pSender, MessageEventHandler::TArgs& args) {
    // ... code ...
  }

}


And finally a derived class which do other "work":

Code: Select all
class CMyChannel : public CEventChannel,
                   public EventsHandler {
public:

  // some Methods

  ERROR_CODE Init() {

    EventsHandler* evHandler = dynamic_cast<EventsHandler*>(this);
    this->RegisterMsg(EMessages::E_MSG_METHOD_MAKE_CALL, Poco::delegate(evHandler,
                                                                        &EventsHandler::OnMsgMethod));

    return EC_NO_ERRORS;
  };
}


where CEventChannel itself derives from MessageEventHandler. I choose this implementation to separate the messages handlers from other code. But, when I run it, I get:

Code: Select all
> MyChannel.dll!std::_Tree<std::_Tmap_traits<int,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > >,std::less<int>,std::allocator<std::pair<int const ,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > > > >,0> >::_Lbound(const int & _Keyval)  Line 2109 + 0xc bytes C++
  MyChannel.dll!std::_Tree<std::_Tmap_traits<int,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > >,std::less<int>,std::allocator<std::pair<int const ,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > > > >,0> >::lower_bound(const int & _Keyval)  Line 1575 + 0x10 bytes    C++
  MyChannel.dll!std::map<int,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > >,std::less<int>,std::allocator<std::pair<int const ,Poco::SharedPtr<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex>,Poco::ReferenceCounter,Poco::ReleasePolicy<Poco::BasicEvent<ESvrd::Protocols::MessageEventHandler::TArgs,Poco::FastMutex> > > > > >::operator[](const int & _Keyval)  Line 226 + 0x10 bytes C++
  MyChannel.dll!ESvrd::Protocols::MessageEventHandler::RegisterMsg<EventsHandler,ESvrd::Protocols::MessageEventHandler::TArgs>(unsigned int msg_opcode, Poco::Delegate<EventsHandler,ESvrd::Protocols::MessageEventHandler::TArgs,1> delegate)  Line 63 + 0x15 bytes    C++

  First-chance exception at 0x0325eaab (MyChannel.dll) in ESvrd.exe: 0xC0000005: Access violation reading location 0xddddddea.
  Unhandled exception at 0x0325eaab (MyChannel.dll) in ESvrd.exe: 0xC0000005: Access violation reading location 0xddddddea.


like the object pointer is not valid.
But, if I eliminate the EventsHandler base class and I put all the code into the MyChannel class all works as expected.


What I missing?

Regards,
Daniele.
dbarzo
 
Posts: 39
Joined: 14 Jan 2013, 17:15
Location: Italy

Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron