cui/source/dialogs/colorpicker.cxx     |    7 ++++---
 include/svx/PaletteManager.hxx         |    2 +-
 svx/source/tbxctrls/PaletteManager.cxx |   11 ++++++-----
 3 files changed, 11 insertions(+), 9 deletions(-)

New commits:
commit 78ccae0d42d168f845ddbd7cb694d80dfb04f84d
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Wed Dec 20 11:21:27 2023 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Fri Dec 22 12:20:27 2023 +0100

    tdf#156820 Fix crash in custom color picker
    
    which requires
    
    (*) keeping ColorPicker alive during the runAsync in
    ColorPicker::startExecuteModal
    
    (*) keeping the PaletteManager alive during the ExecuteAsync in
    PaletteManager::PopupCOlorPicker
    
    Noting that MattK debugged the cause of this.
    
    Change-Id: I07efe8e6514f8882b1347c79c6150578c234bb9d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161054
    Tested-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/cui/source/dialogs/colorpicker.cxx 
b/cui/source/dialogs/colorpicker.cxx
index 87f50b534c26..71b0d0970fc3 100644
--- a/cui/source/dialogs/colorpicker.cxx
+++ b/cui/source/dialogs/colorpicker.cxx
@@ -1345,12 +1345,13 @@ void SAL_CALL ColorPicker::setDialogTitle( const 
OUString& )
 void SAL_CALL ColorPicker::startExecuteModal( const css::uno::Reference< 
css::ui::dialogs::XDialogClosedListener >& xListener )
 {
     std::shared_ptr<ColorPickerDialog> xDlg = 
std::make_shared<ColorPickerDialog>(Application::GetFrameWeld(mxParent), 
mnColor, mnMode);
-    weld::DialogController::runAsync(xDlg, [this, xDlg, xListener] (sal_Int32 
nResult) {
+    rtl::Reference<ColorPicker> xThis(this);
+    weld::DialogController::runAsync(xDlg, [xThis, xDlg, xListener] (sal_Int32 
nResult) {
         if (nResult)
-            mnColor = xDlg->GetColor();
+            xThis->mnColor = xDlg->GetColor();
 
         sal_Int16 nRet = static_cast<sal_Int16>(nResult);
-        css::ui::dialogs::DialogClosedEvent aEvent( *this, nRet );
+        css::ui::dialogs::DialogClosedEvent aEvent( *xThis, nRet );
         xListener->dialogClosed( aEvent );
     });
 }
diff --git a/include/svx/PaletteManager.hxx b/include/svx/PaletteManager.hxx
index f2711b2e022a..81f30ea7de76 100644
--- a/include/svx/PaletteManager.hxx
+++ b/include/svx/PaletteManager.hxx
@@ -33,7 +33,7 @@ namespace svx { class ToolboxButtonColorUpdaterBase; }
 namespace weld { class Window; }
 namespace model { class ColorSet; }
 
-class SVXCORE_DLLPUBLIC PaletteManager
+class SVXCORE_DLLPUBLIC PaletteManager : public 
std::enable_shared_from_this<PaletteManager>
 {
     const sal_uInt16        mnMaxRecentColors;
 
diff --git a/svx/source/tbxctrls/PaletteManager.cxx 
b/svx/source/tbxctrls/PaletteManager.cxx
index 234002006a5e..a98149ff30bb 100644
--- a/svx/source/tbxctrls/PaletteManager.cxx
+++ b/svx/source/tbxctrls/PaletteManager.cxx
@@ -405,15 +405,16 @@ void PaletteManager::PopupColorPicker(weld::Window* 
pParent, const OUString& aCo
     m_pColorDlg = std::make_unique<SvColorDialog>();
     m_pColorDlg->SetColor(rInitialColor);
     m_pColorDlg->SetMode(svtools::ColorPickerMode::Modify);
-    m_pColorDlg->ExecuteAsync(pParent, [this, aCommandCopy] (sal_Int32 
nResult) {
+    std::shared_ptr<PaletteManager> xSelf(shared_from_this());
+    m_pColorDlg->ExecuteAsync(pParent, [xSelf, aCommandCopy] (sal_Int32 
nResult) {
         if (nResult == RET_OK)
         {
-            Color aLastColor = m_pColorDlg->GetColor();
+            Color aLastColor = xSelf->m_pColorDlg->GetColor();
             OUString sColorName = "#" + 
aLastColor.AsRGBHexString().toAsciiUpperCase();
             NamedColor aNamedColor(aLastColor, sColorName);
-            SetSplitButtonColor(aNamedColor);
-            AddRecentColor(aLastColor, sColorName);
-            maColorSelectFunction(aCommandCopy, aNamedColor);
+            xSelf->SetSplitButtonColor(aNamedColor);
+            xSelf->AddRecentColor(aLastColor, sColorName);
+            xSelf->maColorSelectFunction(aCommandCopy, aNamedColor);
         }
     });
 }

Reply via email to