sc/qa/unit/tiledrendering/SheetViewTest.cxx | 46 +++++++++++++++++++++++++++- sc/source/ui/operation/Operation.cxx | 3 + 2 files changed, 48 insertions(+), 1 deletion(-)
New commits: commit b889bc8dbd75d232a9c30a880036d9fc1de1ac2a Author: Tomaž Vajngerl <[email protected]> AuthorDate: Wed Feb 18 17:31:46 2026 +0900 Commit: Miklos Vajna <[email protected]> CommitDate: Wed Feb 18 14:59:34 2026 +0100 sc: Fix a crash because a sheet view can be null When we delete a sheet view, we don't removee it from the vector, but only set it to nullptr. This is needed because the index in a vector is the sheet view ID and we don't want to change all the IDs, but the side effect is that we need to nullptr check. Also a test to make sure this never happens again. Change-Id: I12916addd3dbb1cb85ec9f6ba1671c8207028d0b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/199602 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Miklos Vajna <[email protected]> diff --git a/sc/qa/unit/tiledrendering/SheetViewTest.cxx b/sc/qa/unit/tiledrendering/SheetViewTest.cxx index c5aaeb7f1b34..bd7dfa9d32ab 100644 --- a/sc/qa/unit/tiledrendering/SheetViewTest.cxx +++ b/sc/qa/unit/tiledrendering/SheetViewTest.cxx @@ -141,6 +141,11 @@ protected: dispatchCommand(mxComponent, u".uno:NewSheetView"_ustr, {}); } + void removeSheetViewInCurrentView() + { + dispatchCommand(mxComponent, u".uno:RemoveSheetView"_ustr, {}); + } + void sortAscendingForCell(std::u16string_view aCellAddress) { gotoCell(aCellAddress); @@ -408,7 +413,7 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, testRemoveSheetView) Scheduler::ProcessEventsToIdle(); // We remove the current sheet view - dispatchCommand(mxComponent, u".uno:RemoveSheetView"_ustr, {}); + removeSheetViewInCurrentView(); Scheduler::ProcessEventsToIdle(); // Sheet view is retained, but null @@ -1535,6 +1540,45 @@ CPPUNIT_TEST_FIXTURE(SyncTest, testSync_SheetView_DeleteContentOperation) CPPUNIT_ASSERT_EQUAL(aExpectedSorted, getValues(pDocument, 0, 1, 4, 1)); } +CPPUNIT_TEST_FIXTURE(SyncTest, testCreateAndDeleteSheetView) +{ + // Test that creating a sheet view, deleting it, creating a new one again, + // works without an issue. + + ScModelObj* pModelObj = createDoc("SheetView_AutoFilter.ods"); + pModelObj->initializeForTiledRendering(uno::Sequence<beans::PropertyValue>()); + ScDocument* pDocument = pModelObj->GetDocument(); + + setupViews(); + + // Initial values + CPPUNIT_ASSERT_EQUAL(expectedValues({ u"4", u"5", u"3", u"7" }), + getValues(pDocument, 0, 1, 4, 0)); + + // Create a sheet view + { + switchToSheetView(); + createNewSheetViewInCurrentView(); + } + + // Delete the sheet view + { + switchToSheetView(); + removeSheetViewInCurrentView(); + } + + // Create a new sheet view again + { + switchToSheetView(); + createNewSheetViewInCurrentView(); + + typeCharsInCell(std::string("99"), 0, 1, mpTabViewSheetView, pModelObj); + + CPPUNIT_ASSERT_EQUAL(expectedValues({ u"99", u"5", u"3", u"7" }), + getValues(mpTabViewSheetView, 0, 1, 4)); + } +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/operation/Operation.cxx b/sc/source/ui/operation/Operation.cxx index 8baec262344b..db08818eeacf 100644 --- a/sc/source/ui/operation/Operation.cxx +++ b/sc/source/ui/operation/Operation.cxx @@ -203,6 +203,9 @@ void Operation::syncSheetViews() for (std::shared_ptr<SheetView> const& pSheetView : pManager->getSheetViews()) { + if (!pSheetView) + continue; + SCTAB nSheetViewTab = pSheetView->getTableNumber(); std::optional<ScQueryParam> oQueryParam;
