Skip to content

fix(ResizeWatcher): Fix memory leak with ResizeWatcher #144

Open
sankhesh wants to merge 4 commits intoKitware:masterfrom
sankhesh:resize_memleak
Open

fix(ResizeWatcher): Fix memory leak with ResizeWatcher #144
sankhesh wants to merge 4 commits intoKitware:masterfrom
sankhesh:resize_memleak

Conversation

@sankhesh
Copy link
Copy Markdown
Collaborator

@sankhesh sankhesh commented May 7, 2026

Before

Screenshot_2026-05-07_04-51-20

After

Screenshot_2026-05-07_04-52-57

sankhesh and others added 3 commits May 7, 2026 05:04
Adds a test to detect memory leaks in ResizeWatcher when callbacks are
removed. This test verifies that DOM elements are properly cleaned up.
Unobserves and deletes elements from the callbacks map when they are no
longer needed to resolve memory leaks from stale observer entries.

Co-authored-by: Will McCambley <willmccambley@gmail.com>
Corrects peer dependency flags across dependencies and removes ts-node
and cosmiconfig from package-lock.json files.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a ResizeWatcher memory leak by ensuring elements are fully removed from internal tracking when no callbacks remain, and adds a usage demo to help reproduce/verify the leak behavior over repeated mount/unmount cycles.

Changes:

  • Fix ResizeWatcher.unwatch() to delete element entries from the callbacks map when the last callback is removed.
  • Add a ResizeWatcherLeakTest usage demo and expose ResizeWatcherContext from the library to display tracked-element counts.
  • Update lockfiles (notably the root package-lock.json).

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/utils/ResizeWatcher.ts Fixes leak by deleting tracked elements when no callbacks remain; adds a tracked-element count helper.
src/index.ts Exposes ResizeWatcherContext and related types from the public entrypoint.
usage/src/Tests/ResizeWatcherLeakTest.jsx Adds a manual/visual repro test for the leak via repeated view cycling and tracked-element count display.
usage/src/App.jsx Registers the new usage demo in the demo selector.
usage/package-lock.json Lockfile updated due to dependency tree changes/regeneration.
package-lock.json Lockfile updated, but currently contains invalid version fields (must be fixed).
Files not reviewed (1)
  • usage/package-lock.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils/ResizeWatcher.ts
Comment thread usage/src/Tests/ResizeWatcherLeakTest.jsx Outdated
Comment thread usage/src/Tests/ResizeWatcherLeakTest.jsx
- Add getTrackedElementCount as an optional method on IResizeWatcher
  so TS consumers can call it via the context without casting; keeping
  it optional preserves the freedom for alternate implementations to
  not expose it.
- Wire dependency array on the leak test's tracked-count effect so it
  only re-reads when the watcher or cycle count changes, instead of
  on every render.
- Track the cycle() setTimeout in a ref and clear it on unmount so a
  pending timer cannot setState on an unmounted component if the demo
  is swapped within 200ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants