Migrate native pointer management to SafeHandle#1824
Draft
shimat wants to merge 8 commits intosafehandle_devfrom
Draft
Migrate native pointer management to SafeHandle#1824shimat wants to merge 8 commits intosafehandle_devfrom
shimat wants to merge 8 commits intosafehandle_devfrom
Conversation
shimat
commented
Feb 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace raw
IntPtrpointer management inDisposableCvObjectand all derived classes withSafeHandle-based resource tracking. Theptrfield is now a read-only computed property backed by a singleSafeHandleinstance, eliminating an entire class of use-after-free and double-free bugs.Motivation
The previous design stored native pointers in a mutable
protected IntPtr ptrfield. This made it possible to:SafeHandleis the .NET-recommended pattern for wrapping native resources. It provides atomic release guarantees, prevents handle recycling attacks, and integrates with the CLR's P/Invoke marshalling layer.Design
Core change in
DisposableCvObject:ptris nowprotected IntPtr ptr => safeHandle?.DangerousGetHandle() ?? IntPtr.Zero;(read-only)OpenCvSafeHandle? safeHandlefield is the single source of truthSetSafeHandle()replaces the handle and invalidates any previous oneDisposeManaged()disposes the SafeHandleNew types:
OpenCvSafeHandle— abstract base (SafeHandlewhereIntPtr.Zerois invalid)OpenCvPtrSafeHandle— delegate-based handle that calls a suppliedAction<IntPtr>on release, avoiding the need for hundreds of per-type SafeHandle subclassesMatSafeHandle— dedicated SafeHandle forMat(the most frequently used type)Inner
Ptrclasses (cv::Ptr<T> wrappers):Ptrbase constructor now acceptsAction<IntPtr>? releaseActionPtrclasses pass their delete function as a static lambda:DisposeUnmanaged()overrides that only calledbase.DisposeUnmanaged()have been removedNon-owning handles:
InputArray,VectorOf*, andKalmanFilterthat manage their own delete calls inDisposeUnmanaged()useownsHandle: falseSafeHandles (the handle just stores the pointer value)Changes
OpenCvSafeHandle.cs,OpenCvPtrSafeHandle.cs,MatSafeHandle.csptr =direct writes eliminated — no code can assign toptranymoreout ptrpatterns converted toout var p+SetSafeHandle().github/copilot-instructions.md(UTF-8 BOM encoding policy)Breaking changes
None for public API consumers. The
ptrproperty isprotected, so only subclasses are affected. Any external subclass ofDisposableCvObjectthat was writing toptrdirectly will need to callSetSafeHandle()instead.