Force deletion of slot in boost::signals2

前端 未结 5 1167
清歌不尽
清歌不尽 2021-02-05 20:13

I have found that boost::signals2 uses sort of a lazy deletion of connected slots, which makes it difficult to use connections as something that manages lifetimes of objects. I

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 21:06

    boost::signals2 does clean up the slots during connect/invoke.

    So if all the slots disconnect themselves from the signal, invoking the signal a second time will not call anything but it should clean up the slots.

    To answer your comment, yes, invoking the signal again is not safe if there are be other slots connected, as they will be invoked again. In that case I suggest you go the other way around and connect a dummy slot, then disconnect it when your "real" slot is invoked. Connecting another slot will clean up stale connections, so your slot should be released.

    Just make sure that you don't keep any references that need freeing in the dummy slot, or you're back where you started.

提交回复
热议问题