clutter/actor: First clear all stage views before emitting they changed
If one would end up with an actor attached to mapped actor, where the attached actor doesn't itself have an up to date stage view list while listening on the stage for updating, when clearing the stage views of the list, anything that would query the stage views list at this time would end up accessing freed memory.
This could happen if
- An actor was added to a newly created container actor attached to the stage
- The actor got a timeline attached to it
- The actor was moved to a container that already was mapped
- A hotplug happened
After (1) both the container and actor would not have any stage views. After (2) the timeline would listen on the stage for stage views updates. After (3) the actor would still listen on the stage for stage views updates. When (4) happened, the actor would be signalled when the stage got its stage view cleared, at which point it would traverse up its actor's tree finding an appropriate stage view to base its animation on. The problem here would be that it'd query the already mapped container and its yet-to-be-cleared stage view list, resulting in use-after free, resulting in for example the following backtrace:
- g_type_check_instance_cast ()
- CLUTTER_STAGE_VIEW ()
- clutter_actor_pick_frame_clock ()
- clutter_actor_pick_frame_clock ()
- update_frame_clock ()
- on_frame_clock_actor_stage_views_changed ()
- g_closure_invoke ()
- signal_emit_unlocked_R ()
- g_signal_emit_valist ()
- g_signal_emit ()
- clear_stage_views_cb ()
- _clutter_actor_traverse_depth ()
- _clutter_actor_traverse ()
- clutter_actor_clear_stage_views_recursive ()
- clutter_stage_clear_stage_views () ...
Avoid this issue by making sure that we don't emit 'stage-views-changed' signals while the actor tree is in an invalid state. While we now end up traversing tree twice, it doesn't change the Big-O notation. It has not been measured whether this has any noticible performance impact.
Closes: #1950 (closed)