Skip to content
  • Vlad Zahorodnii's avatar
    [x11] Fix crash during tear down · d2bbd2a1
    Vlad Zahorodnii authored
    Summary:
    Any call made to a virtual method in constructor/destructor of a base
    class won't go to a derived class because the base class may access
    uninitialized or destroyed resources.
    
    For example, let's consider the following two classes
    
        class Base {
        public:
            Base() { foo()->bar(); }
            virtual ~Base() { foo()->bar(); }
    
            virtual Foo* foo() const { return nullptr; }
        };
    
        class Derived : public Base {
        public:
            Derived() : mFoo(new Foo) {}
            ~Derived() override { delete mFoo; }
    
            Foo* foo() const override { return mFoo; }
    
        private:
            Foo* mFoo;
        };
    
    When an instance of Derived class is created, constructors will run in
    the following order:
    
        Base()
        Derived()
    
    It's not safe to dispatch foo() method call to Derived class because
    constructor of Derived hasn't initialized yet mFoo.
    
    Same story with destructors, they'll run in the following order:
    
        ~Derived()
        ~Base()
    
    It's not safe to dispatch foo() method call in the destructor of Base
    class to Derived class because mFoo was deleted.
    
    So, what does that weird C++ behavior has something to do with KWin? Well,
    recently Compositor class was split into two classes - WaylandCompositor,
    and X11Compositor. Some functionality from X11 doesn't make sense on
    Wayland. Therefore methods that implement that stuff were "purified," i.e.
    they became pure virtual methods. Unfortunately, when Compositor tears
    down it may call pure virtual methods on itself. Given that those calls
    cannot be dispatched to X11Compositor or WaylandCompositor, the only
    choice that C++ runtime has is to throw an exception.
    
    The fix for this very delicate problem is very simple - do not call virtual
    methods from constructors and the destructor. Avoid doing that if you can!
    
    This change moves Compositor::updateClientCompositeBlocking to X11Compositor
    so it longer has to be a virtual method. Also, it kind of doesn't make sense
    to keep it in base Compositor class because compositing can be blocked only
    on X11.
    
    BUG: 411049
    
    Test Plan: KWin no longer crashes when running kwin_x11 --replace command.
    
    Reviewers: #kwin, romangg
    
    Reviewed By: #kwin, romangg
    
    Subscribers: anthonyfieroni, kwin
    
    Tags: #kwin
    
    Differential Revision: https://phabricator.kde.org/D23098
    d2bbd2a1