Page 1 of 1

Unsubscribing to events : Always needed or not?

Posted: Tue Apr 24, 2012 11:01
by matt
Hello!

I am using CEGUI (Wonderfull tool;) ) for a few month yet, and until now, I never cared about unsubscribing to events.
For the moment I am creating a custom widget (a carousel), that contains a ribbon, on which I can dynamically add some button.
I can clear the Carousel, which in turns delete the ribbon which contains the buttons, and add a new one.
The problem is that my program crash during this operation.

So I tried to clear my events subscription before deleting the object for which an event has been subscribed (Do I really need to do this indeed?)

I do like this, but it seems quite painfull. Is it the only solution?

Code: Select all

void Carousel::createNewRibbon()
{
   WindowManager& wmgr = WindowManager::getSingleton();
   m_ribbon= static_cast<Ribbon*>(wmgr.createWindow( "MyWidgets/Ribbon"));

   m_ribbonEventConnection = m_ribbon->subscribeEvent(
      Carousel::EventClicked,
      Event::Subscriber(&Carousel::ribbonClickedEv, this));
}


And when I clear the ribbon, and add a new one, I do like this....

Code: Select all

void Carousel::clear()
{
   if(m_ribbon)
   {
      if(m_ribbonEventConnection )m_ribbonEventConnection ->disconnect();
      delete m_ribbon;
      m_ribbon= NULL;
   }

   createNewRibbon(); //
}



Indeed, I would need to "unbind" (unsubscribe), each little button on by ribbon? It seems very complicated, because I need to store a "connection" for each little button?

  • Are the connection automatticaly deleted, when objects are deleted?
  • Shall we always remove by hand the connections, in order to keep good performances?

Thanx in advance for your help

Re: Unsubscribing to events : Always needed or not?

Posted: Mon Apr 30, 2012 08:35
by CrazyEddie
It's not always needed, no.

Basically, when you delete a widget, any and all event connections are automatically destroyed along with it. Not sure what the crash was you were seeing, you'd need to supply some diagnostic info.

For connection management in the more general sense, when you don't always delete the window but need to manage connections manually, you can either do it the 100% manual way of calling disconnect, or there is a Event::ScopedConnection that you can use which wraps the Event::Connection and automatically disconnects the thing when the ScopedConnection goes out of scope. This is good for use as class data / in collections to 'automatically' clean up.

HTH

CE.

Re: Unsubscribing to events : Always needed or not?

Posted: Fri May 04, 2012 14:32
by matt
ok! :)
Thanx for the answer