Unsubscribing to events : Always needed or not?

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

matt
Not too shy to talk
Not too shy to talk
Posts: 37
Joined: Tue Nov 08, 2011 16:00

Unsubscribing to events : Always needed or not?

Postby matt » Tue Apr 24, 2012 11:01

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

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Unsubscribing to events : Always needed or not?

Postby CrazyEddie » Mon Apr 30, 2012 08:35

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.

matt
Not too shy to talk
Not too shy to talk
Posts: 37
Joined: Tue Nov 08, 2011 16:00

Re: Unsubscribing to events : Always needed or not?

Postby matt » Fri May 04, 2012 14:32

ok! :)
Thanx for the answer


Return to “Help”

Who is online

Users browsing this forum: No registered users and 10 guests