Fix crash in angle item

Updating preferences caused stale VIEW items that could crash the
system.  Changed this to weak references and shared ownership of edit
points to avoid stale view items
This commit is contained in:
Seth Hillbrand
2026-01-08 11:54:19 -08:00
parent 6a8afd5b19
commit 5fc0646309
7 changed files with 106 additions and 19 deletions
+16 -12
View File
@@ -35,7 +35,7 @@
using namespace KIGFX::PREVIEW;
ANGLE_ITEM::ANGLE_ITEM( EDIT_POINTS* aPoints ) :
ANGLE_ITEM::ANGLE_ITEM( const std::shared_ptr<EDIT_POINTS>& aPoints ) :
SIMPLE_OVERLAY_ITEM(),
m_points( aPoints )
{
@@ -43,15 +43,19 @@ ANGLE_ITEM::ANGLE_ITEM( EDIT_POINTS* aPoints ) :
const BOX2I ANGLE_ITEM::ViewBBox() const
{
if( m_points )
return m_points->ViewBBox();
else
return BOX2I();
std::shared_ptr<EDIT_POINTS> points = m_points.lock();
if( points )
return points->ViewBBox();
return BOX2I();
}
void ANGLE_ITEM::drawPreviewShape( KIGFX::VIEW* aView ) const
{
if( !m_points )
std::shared_ptr<EDIT_POINTS> points = m_points.lock();
if( !points )
return;
KIGFX::GAL* gal = aView->GetGAL();
@@ -70,16 +74,16 @@ void ANGLE_ITEM::drawPreviewShape( KIGFX::VIEW* aView ) const
std::vector<const EDIT_POINT*> anglePoints;
for( unsigned i = 0; i < m_points->PointsSize(); ++i )
for( unsigned i = 0; i < points->PointsSize(); ++i )
{
const EDIT_POINT& point = m_points->Point( i );
const EDIT_POINT& point = points->Point( i );
if( point.IsActive() || point.IsHover() )
{
anglePoints.push_back( &point );
EDIT_POINT* prev = m_points->Previous( point );
EDIT_POINT* next = m_points->Next( point );
EDIT_POINT* prev = points->Previous( point );
EDIT_POINT* next = points->Next( point );
if( prev )
anglePoints.push_back( prev );
@@ -110,8 +114,8 @@ void ANGLE_ITEM::drawPreviewShape( KIGFX::VIEW* aView ) const
for( const EDIT_POINT* pt : anglePoints )
{
EDIT_POINT* prev = m_points->Previous( *pt );
EDIT_POINT* next = m_points->Next( *pt );
EDIT_POINT* prev = points->Previous( *pt );
EDIT_POINT* next = points->Next( *pt );
if( !( prev && next ) )
continue;
+11 -1
View File
@@ -1022,6 +1022,16 @@ void SCH_POINT_EDITOR::Reset( RESET_REASON aReason )
{
SCH_TOOL_BASE::Reset( aReason );
if( KIGFX::VIEW* view = getView() )
{
if( m_angleItem )
view->Remove( m_angleItem.get() );
if( m_editPoints )
view->Remove( m_editPoints.get() );
}
m_angleItem.reset();
m_editPoints.reset();
m_editedPoint = nullptr;
}
@@ -1135,7 +1145,7 @@ int SCH_POINT_EDITOR::Main( const TOOL_EVENT& aEvent )
controls->ShowCursor( true );
makePointsAndBehavior( item );
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints.get() );
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints );
view->Add( m_editPoints.get() );
view->Add( m_angleItem.get() );
setEditedPoint( nullptr );
+5 -3
View File
@@ -24,6 +24,8 @@
#ifndef PREVIEW_ANGLE_ITEM_H
#define PREVIEW_ANGLE_ITEM_H
#include <memory>
#include <preview_items/simple_overlay_item.h>
class EDIT_POINTS;
@@ -36,11 +38,11 @@ namespace PREVIEW
class ANGLE_ITEM : public SIMPLE_OVERLAY_ITEM
{
public:
ANGLE_ITEM( EDIT_POINTS* aPoints );
ANGLE_ITEM( const std::shared_ptr<EDIT_POINTS>& aPoints );
const BOX2I ViewBBox() const override;
void SetEditPoints( EDIT_POINTS* aPoints )
void SetEditPoints( const std::shared_ptr<EDIT_POINTS>& aPoints )
{
m_points = aPoints;
}
@@ -48,7 +50,7 @@ public:
private:
void drawPreviewShape( KIGFX::VIEW* aView ) const override;
EDIT_POINTS* m_points;
std::weak_ptr<EDIT_POINTS> m_points;
};
} // PREVIEW
+11 -1
View File
@@ -126,6 +126,16 @@ void PL_POINT_EDITOR::Reset( RESET_REASON aReason )
m_frame = getEditFrame<PL_EDITOR_FRAME>();
}
if( KIGFX::VIEW* view = getView() )
{
if( m_angleItem )
view->Remove( m_angleItem.get() );
if( m_editPoints )
view->Remove( m_editPoints.get() );
}
m_angleItem.reset();
m_editPoints.reset();
}
@@ -177,7 +187,7 @@ int PL_POINT_EDITOR::Main( const TOOL_EVENT& aEvent )
if( !m_editPoints )
return 0;
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints.get() );
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints );
getView()->Add( m_editPoints.get() );
getView()->Add( m_angleItem.get() );
+15 -2
View File
@@ -1808,6 +1808,19 @@ PCB_POINT_EDITOR::PCB_POINT_EDITOR() :
void PCB_POINT_EDITOR::Reset( RESET_REASON aReason )
{
m_frame = getEditFrame<PCB_BASE_FRAME>();
if( KIGFX::VIEW* view = getView() )
{
if( m_angleItem )
view->Remove( m_angleItem.get() );
if( m_editPoints )
view->Remove( m_editPoints.get() );
view->Remove( &m_preview );
}
m_angleItem.reset();
m_editPoints.reset();
m_altConstraint.reset();
getViewControls()->SetAutoPan( false );
@@ -2225,7 +2238,7 @@ int PCB_POINT_EDITOR::OnSelectionChange( const TOOL_EVENT& aEvent )
// Only add the angle_item if we are editing a polygon or zone
if( item->Type() == PCB_ZONE_T || ( graphicItem && graphicItem->GetShape() == SHAPE_T::POLY ) )
{
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints.get() );
m_angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( m_editPoints );
}
m_preview.FreeItems();
@@ -2694,7 +2707,7 @@ int PCB_POINT_EDITOR::OnSelectionChange( const TOOL_EVENT& aEvent )
if( m_angleItem )
{
m_angleItem->SetEditPoints( m_editPoints.get() );
m_angleItem->SetEditPoints( m_editPoints );
getView()->Add( m_angleItem.get() );
}
+2
View File
@@ -30,6 +30,8 @@ set( QA_COMMON_SRCS
wximage_test_utils.cpp
test_angle_item.cpp
test_array_axis.cpp
test_base_set.cpp
test_bitmap_base.cpp
+46
View File
@@ -0,0 +1,46 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <memory>
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <preview_items/angle_item.h>
#include <tool/edit_points.h>
BOOST_AUTO_TEST_SUITE( AngleItem )
BOOST_AUTO_TEST_CASE( IgnoresExpiredEditPoints )
{
std::unique_ptr<KIGFX::PREVIEW::ANGLE_ITEM> angleItem;
{
auto editPoints = std::make_shared<EDIT_POINTS>( nullptr );
angleItem = std::make_unique<KIGFX::PREVIEW::ANGLE_ITEM>( editPoints );
}
BOOST_CHECK_NO_THROW( angleItem->ViewBBox() );
}
BOOST_AUTO_TEST_SUITE_END()