tdf#154629 inspector sidebar panel use-after-free on switch to print preview

Change-Id: I5489cd18213d82ae4174ca8d9d00f1da5aa1091d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150078
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx b/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
index 5f86100..8786a03 100644
--- a/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
+++ b/sw/source/uibase/sidebar/WriterInspectorTextPanel.cxx
@@ -70,6 +70,10 @@ WriterInspectorTextPanel::WriterInspectorTextPanel(weld::Widget* pParent)
    {
        m_oldLink = m_pShell->GetChgLnk();
        m_pShell->SetChgLnk(LINK(this, WriterInspectorTextPanel, AttrChangedNotify));

        // tdf#154629 listen to know if the shell destructs before this panel does,
        // which can happen on entering print preview
        m_pShell->Add(this);
    }

    // Update panel on start
@@ -80,10 +84,24 @@ WriterInspectorTextPanel::WriterInspectorTextPanel(weld::Widget* pParent)
    updateEntries(aStore, m_nParIdx);
}

void WriterInspectorTextPanel::SwClientNotify(const SwModify& rModify, const SfxHint& rHint)
{
    if (rHint.GetId() == SfxHintId::SwLegacyModify)
    {
        const sw::LegacyModifyHint& rLegacy = static_cast<const sw::LegacyModifyHint&>(rHint);
        if (rLegacy.GetWhich() == RES_OBJECTDYING)
            m_pShell = nullptr;
    }
    SwClient::SwClientNotify(rModify, rHint);
}

WriterInspectorTextPanel::~WriterInspectorTextPanel()
{
    if (m_pShell)
    {
        m_pShell->SetChgLnk(m_oldLink);
        m_pShell->Remove(this);
    }
}

static OUString PropertyNametoRID(const OUString& rName)
diff --git a/sw/source/uibase/sidebar/WriterInspectorTextPanel.hxx b/sw/source/uibase/sidebar/WriterInspectorTextPanel.hxx
index fae6190..2c2df34 100644
--- a/sw/source/uibase/sidebar/WriterInspectorTextPanel.hxx
+++ b/sw/source/uibase/sidebar/WriterInspectorTextPanel.hxx
@@ -19,12 +19,13 @@
#pragma once

#include <svx/sidebar/InspectorTextPanel.hxx>
#include <calbck.hxx>

class SwWrtShell;

namespace sw::sidebar
{
class WriterInspectorTextPanel final : public svx::sidebar::InspectorTextPanel
class WriterInspectorTextPanel final : public svx::sidebar::InspectorTextPanel, public SwClient
{
public:
    static std::unique_ptr<PanelLayout> Create(weld::Widget* pParent);
@@ -40,6 +41,8 @@ private:

    // attributes have changed
    DECL_LINK(AttrChangedNotify, LinkParamNone*, void);

    virtual void SwClientNotify(const SwModify&, const SfxHint& rHint) override;
};

} // end of namespace svx::sidebar