[Bugfix] GridChunk::parent_grid raw pointer can dangle #277

Closed
opened 2026-03-07 23:25:45 +00:00 by john · 2 comments
Owner

Summary

GridChunk stores its parent grid as a raw UIGrid* pointer at GridChunk.h:39. This is the same class of bug as GridLayer::parent_grid (#270) — the raw pointer can dangle if the grid is destroyed while chunks are still referenced.

Root Cause

GridChunk.h:39:

class GridChunk {
    // ...
    UIGrid* parent_grid;   // Raw pointer
    // ...
};

And the parallel structure at line 72:

// Another GridChunk variant (likely for entity layer)
UIGrid* parent_grid;   // Also raw

Risk

GridChunks are owned by the grid, so under normal operation they don't outlive their parent. However:

  1. If any code path takes a pointer/reference to a GridChunk and the grid is destroyed, the parent_grid pointer dangles
  2. The inconsistency between raw pointers (GridChunk, GridLayer, UIGridPoint) and shared_ptr patterns used elsewhere makes the codebase harder to reason about

Fix

Same as #270 — convert to std::weak_ptr<UIGrid> or document the ownership invariant. Since GridChunks are always owned by grids and never exposed to Python, this is lower priority than the GridLayer and UIGridPoint cases.

Severity

Low — GridChunks are internal to UIGrid and not exposed to Python, so they generally can't outlive their parent grid. Fixing this is more about consistency than preventing crashes.

  • #270 — GridLayer::parent_grid (same pattern, higher risk because layers are Python-accessible)
  • #271 — UIGridPoint::parent_grid (same pattern)
## Summary `GridChunk` stores its parent grid as a raw `UIGrid*` pointer at `GridChunk.h:39`. This is the same class of bug as `GridLayer::parent_grid` (#270) — the raw pointer can dangle if the grid is destroyed while chunks are still referenced. ## Root Cause `GridChunk.h:39`: ```cpp class GridChunk { // ... UIGrid* parent_grid; // Raw pointer // ... }; ``` And the parallel structure at line 72: ```cpp // Another GridChunk variant (likely for entity layer) UIGrid* parent_grid; // Also raw ``` ## Risk GridChunks are owned by the grid, so under normal operation they don't outlive their parent. However: 1. If any code path takes a pointer/reference to a GridChunk and the grid is destroyed, the `parent_grid` pointer dangles 2. The inconsistency between raw pointers (GridChunk, GridLayer, UIGridPoint) and shared_ptr patterns used elsewhere makes the codebase harder to reason about ## Fix Same as #270 — convert to `std::weak_ptr<UIGrid>` or document the ownership invariant. Since GridChunks are always owned by grids and never exposed to Python, this is lower priority than the GridLayer and UIGridPoint cases. ## Severity **Low** — GridChunks are internal to UIGrid and not exposed to Python, so they generally can't outlive their parent grid. Fixing this is more about consistency than preventing crashes. ## Related - #270 — GridLayer::parent_grid (same pattern, higher risk because layers are Python-accessible) - #271 — UIGridPoint::parent_grid (same pattern)
Author
Owner

Deferred from the #258–#278 memory safety audit. GridChunk is internal to UIGrid and not Python-accessible, so the risk is low. In #252, the plan states GridChunk.h/cpp gets removed entirely — "GridPoint storage replaced by DiscreteMap slots." The chunk rendering architecture continues for layers but GridChunk's parent_grid raw pointer ceases to exist. Linked as dependency of #252.

Deferred from the #258–#278 memory safety audit. GridChunk is internal to UIGrid and not Python-accessible, so the risk is low. In #252, the plan states GridChunk.h/cpp gets removed entirely — "GridPoint storage replaced by DiscreteMap slots." The chunk rendering architecture continues for layers but GridChunk's `parent_grid` raw pointer ceases to exist. Linked as dependency of #252.
Author
Owner

Roadmap context

Folded into #252 (Grid/GridView split) in the Grid & Entity Overhaul Roadmap (docs/GRID_ENTITY_OVERHAUL_ROADMAP.md), Phase 4. Raw UIGrid* in GridChunk will become weak_ptr<Grid> during the architecture overhaul.

## Roadmap context Folded into #252 (Grid/GridView split) in the Grid & Entity Overhaul Roadmap (`docs/GRID_ENTITY_OVERHAUL_ROADMAP.md`), **Phase 4**. Raw `UIGrid*` in GridChunk will become `weak_ptr<Grid>` during the architecture overhaul.
john closed this issue 2026-04-10 05:46:54 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
john/McRogueFace#277
No description provided.