Skip to content

Modernize Window/CvTrackbar; delete DisposableObject (closes #1941) - #1972

Merged
shimat merged 1 commit into
5.xfrom
feature/highgui-window-modernize-5x
Jun 28, 2026
Merged

shimat merged 1 commit into
5.xfrom
feature/highgui-window-modernize-5x

Conversation

@shimat

@shimat shimat commented Jun 28, 2026

Copy link
Copy Markdown
Owner

@
Final slice of #1941 (part of #1938). Stacked on #1970 (review/merge order: #1968 → #1969 → #1970 → this). Migrates the last two DisposableObject subclasses off it and removes DisposableObject entirely — the goal of #1941.

Decision (with @shimat): keep the OO Window/CvTrackbar API (it has value as an object form of OpenCVs name-keyed highgui), but modernize it. OpenCV 5s highgui design is unchanged (name-based namedWindow/imshow/setMouseCallback/createTrackbar, with MouseCallback/TrackbarCallback typedefs), so the wrappers premise is stable.

Centralized callback lifetime (also fixes a latent crash)

OpenCV highgui stores raw function pointers per window name and invokes them later from its own UI loop, so the managed delegates must stay alive until the window is destroyed. Cv2.SetMouseCallback / Cv2.CreateTrackbar previously passed the delegate straight to native without rooting it — the GC could collect a delegate OpenCV still calls → crash.

Added a name-keyed registry in Cv2 that roots mouse/trackbar delegates and clears them on DestroyWindow / DestroyAllWindows. This mirrors OpenCVs own name-keyed, process-global model and is now the single place these delegates are rooted.

Window

  • Now sealed class : IDisposable (off DisposableObject); no finalizer.
  • Removed dead code: the MouseCallback property + ScopedGCHandle machinery (its setter was never called anywhere) and the NotImplementedException DisplayOverlay/DisplayStatusBar stubs.
  • Dispose routes through Cv2.DestroyWindow, which also forgets the windows callbacks.

CvTrackbar

  • Now a plain sealed class (no IDisposable). It previously held two GCHandles solely to keep the callback delegates alive — the Cv2 registry now does that. Creation routes through Cv2.CreateTrackbar; no GCHandle, no GetFunctionPointerForDelegate.

Cleanup

  • Deleted DisposableObject (no remaining subclasses) and the now-unused ScopedGCHandle helper.

Notes

  • Window.GetWindowByName and the static Dictionary<string, Window> registry are kept as-is (still public API). They remain a minor global-state smell but are out of scope here; can be revisited separately.
  • Managed + test projects build clean. highgui runtime behavior validated on CI.
    @

Final slice of #1941. Migrates the last two DisposableObject subclasses off it
and removes the base class entirely.

Centralized callback lifetime (also fixes a latent crash):
- OpenCV highgui stores raw function pointers per window name and calls them
  later, so the managed delegates must outlive the call. Cv2.SetMouseCallback /
  Cv2.CreateTrackbar previously passed the delegate straight to native without
  rooting it, so the GC could collect a delegate OpenCV still invoked. Add a
  name-keyed registry in Cv2 that roots mouse/trackbar delegates and clears them
  on DestroyWindow / DestroyAllWindows.

Window:
- now `sealed class : IDisposable` (off DisposableObject), no finalizer.
- delete the dead MouseCallback property + ScopedGCHandle machinery (its setter
  was never called) and the NotImplemented DisplayOverlay/DisplayStatusBar stubs.
- Dispose routes through Cv2.DestroyWindow, which also forgets the callbacks.

CvTrackbar:
- now a plain `sealed class` (no IDisposable): it held two GCHandles purely to
  keep the callback delegates alive, which the Cv2 registry now does. It routes
  creation through Cv2.CreateTrackbar; no GCHandle, no GetFunctionPointerForDelegate.

Cleanup:
- delete DisposableObject (no remaining subclasses) and the now-unused
  ScopedGCHandle helper.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Base automatically changed from feature/arrayaddress-refstruct-5x to 5.x June 28, 2026 10:05
@shimat shimat self-assigned this Jun 28, 2026
@shimat
shimat merged commit a4b2ba6 into 5.x Jun 28, 2026
14 checks passed
@shimat
shimat deleted the feature/highgui-window-modernize-5x branch June 28, 2026 11:10
@shimat shimat added the enhancement New feature or improvement to OpenCvSharp label Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement to OpenCvSharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant