Connecting signal to destruction of an object - or - self-destructing handlers



How/can I receive an event when some object of my choice is destroyed, so that I can take appropriate action?

Here's an example I came up against lately. I should probably refactor the code so I don't need to do things this way... but it illustrates the question quite well. I have a ComboBox where, via some boring exposition, I want to put a signal_changed() handler on its StyleContext. However, that handler holds a reference to a row in the ComboBox's model. Therefore, I need to receive notification when that model dies (i.e. when all holders of RefPtrs release them), so that I can disconnect that signal_changed() handler and therefore stop it from trying to access a dead row.

Is sigc::trackable::add_destroy_notify_cb() an option here, or perhaps the only one? I didn't try it yet because it looks quite low-level and I thought I would check whether there's anything better higher up in the stack. I didn't find anything on a quick skim, though.

Alternatively, is there any way to have signal handlers disconnect themselves? This is something I have wanted several times but have had to resort to ugly hacks. (We obviously can't say 'assign this sigc::connection from this lambda, but also refer to the post-assignment connection in the lambda.)

Thanks in advance for any thoughts.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]