Skip to content

Add IMallocSpy interface - Prepare for better memory leak detection. - #997

Merged
junkmd merged 3 commits into
enthought:mainfrom
junkmd:reg_revoke_mallocspy
Nov 17, 2026
Merged

junkmd merged 3 commits into
enthought:mainfrom
junkmd:reg_revoke_mallocspy

Conversation

@junkmd

@junkmd junkmd commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Related to #971

Summary

Add the IMallocSpy COM interface and its registration functions (CoRegisterMallocSpy, CoRevokeMallocSpy) to malloc.py. Add full test coverage in test/test_malloc.py.

Why

The previous two PRs (#995 and #996) improved find_memleak.

But find_memleak still has a limit: it only sees the total memory of the process.
It cannot tell which allocation leaked.
If a test fails, we know memory grew, but we do not know where the leak is.

IMallocSpy solves this. It is a standard COM interface.
When you register a spy, COM calls your hook functions before and after every memory operation (Alloc, Free, Realloc, GetSize, DidAlloc, HeapMinimize).
The spy sees every single allocation and free, with the exact size and pointer.

By adding IMallocSpy now, we prepare the codebase for the next step: a per-allocation leak detector. This future tool will:

  • Record every allocation (size, pointer, call stack)
  • Match each Free to its Alloc
  • Report allocations that were never freed
  • Show exactly where the leak happened

This is a small, safe change. It only adds the interface definition and registration. It does not change any existing behavior.
The tests prove the hooks fire in the correct order and with the correct arguments.

Why this is the right preparation

  • No behavior change: Existing code works exactly as before.
  • Standard COM: IMallocSpy is a documented Microsoft interface. No custom hacks.
  • Test-first: The tests verify the spy mechanism works correctly before we build on it.
  • Foundation: The next PR can build a leak detector on top of this solid base.

Out of Scope

  • Building the actual per-allocation leak detector (next PR).
  • Making IMallocSpy active by default.
  • Changing find_memleak to use the spy (that comes later).

Trade-offs

Pros:

  • Enables precise leak diagnosis in the future.
  • Uses standard Windows COM mechanism.
  • Tests give confidence the hooks work.

Cons:

  • Adds ~100 lines of interface definition.
  • Adds ~250 lines of tests.
  • No immediate user-visible benefit (preparation only).

Justification

This is a preparatory refactor.
The motivation is clear: the previous PRs made leak detection stronger at the process level.
The natural next step is allocation-level detection. IMallocSpy is the Windows-provided tool for that.
Adding it now, with full tests, makes the next step easy and safe.

@junkmd junkmd added this to the 1.5.1 milestone Nov 17, 2026
@junkmd junkmd added enhancement New feature or request tests enhance or fix tests labels Nov 17, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.84%. Comparing base (dbdbe10) to head (edd0b0d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #997      +/-   ##
==========================================
+ Coverage   91.71%   91.84%   +0.12%     
==========================================
  Files         150      150              
  Lines       15424    15668     +244     
==========================================
+ Hits        14146    14390     +244     
  Misses       1278     1278              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@junkmd
junkmd merged commit fd37c96 into enthought:main Nov 17, 2026
133 of 135 checks passed
@junkmd
junkmd deleted the reg_revoke_mallocspy branch November 17, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request tests enhance or fix tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants