Skip to content

Migrate native pointer management to SafeHandle and fix related issues - #1846

Merged
shimat merged 25 commits into
mainfrom
safehandle_dev
Mar 29, 2026
Merged

shimat merged 25 commits into
mainfrom
safehandle_dev

Conversation

@shimat

@shimat shimat commented Mar 29, 2026

Copy link
Copy Markdown
Owner

This pull request introduces significant refactoring to the OpenCvSharp resource management model, primarily by removing the legacy DisposableCvObject and ICvPtrHolder patterns and replacing them with two new base classes: CvObject and CvPtrObject. These changes improve clarity and safety around native pointer ownership, especially for OpenCV objects managed by smart pointers. Additionally, there are minor updates to build scripts, package versions, and documentation.

Key changes include:

Resource Management Refactor

  • Removed DisposableCvObject and ICvPtrHolder, replacing them with a new CvObject base class that uses OpenCvSafeHandle for native pointer management, improving resource safety and clarity. ([[1]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-5a18a86add21196c04085f3cc9aeb198feb32a195772a6cab30cfbc411de55f8L1-L73), [[2]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-cbd116849fbc0c45be4778d522a100876f3ef804753c334089fa226da5a43665L1-L12), [[3]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-0ba9481b71a9c57ee83ca39f667de7a4d5b57eae911be922933a0ed849903f59L1-R109))
  • Introduced CvPtrObject as a new base class for OpenCV objects managed by smart pointers (cv::Ptr<T>), ensuring correct lifetime and pointer access for both the smart pointer and the underlying raw pointer. ([src/OpenCvSharp/Fundamentals/CvPtrObject.csR1-R75](https://github.com/shimat/opencvsharp/pull/1846/files#diff-9a083bf024caee96256d126347a6fdde97a98987b35babf47cb5241570424384R1-R75))
  • Updated pointer conversion helpers in Cv2 to use the new base classes, introducing ToPtr(CvObject?) and ToPtr(CvPtrObject?). ([src/OpenCvSharp/Cv2/Cv2.csL30-R33](https://github.com/shimat/opencvsharp/pull/1846/files#diff-25db53258b4a54b4c23f03a357d6646c03a149e32fab5d33b3bbbd014aa7aa18L30-R33))
  • Added MatSafeHandle for safe management of cv::Mat* pointers, encapsulating deletion logic. ([src/OpenCvSharp/Fundamentals/MatSafeHandle.csR1-R37](https://github.com/shimat/opencvsharp/pull/1846/files#diff-45daa87e95124b655918ddd6c76fa5f64b24da36fa0310aa313ccb271433a094R1-R37))

Build and Dependency Updates

  • Updated NuGet package versions for System.Drawing.Common and Microsoft.SourceLink.GitHub in project files to newer releases. ([[1]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-0bf6d2ce4ab0de5af0333f2b005060a6df397e3bb4eef061845fa28c4b57b7d9L53-R58), [[2]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-427fd9d7b7e9a4aa01c7c2275ac551901889ad3eb03eec7b98f7aceff98e23b0L42-R42))
  • Improved the Windows build script (build_opencv_windows.ps1) to handle Visual Studio detection more robustly, ensure UTF-8 output, and always remove stale CMakeCache.txt before configuration. ([[1]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-24f90879f4d1176202e0b743eb44560e1894a0cf4fc72a4c78317ce933b6bfcbR23), [[2]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-24f90879f4d1176202e0b743eb44560e1894a0cf4fc72a4c78317ce933b6bfcbL66-R82), [[3]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-24f90879f4d1176202e0b743eb44560e1894a0cf4fc72a4c78317ce933b6bfcbR115-R126), [[4]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-24f90879f4d1176202e0b743eb44560e1894a0cf4fc72a4c78317ce933b6bfcbR146))
  • Updated CMake configuration to use modern CMake variables for C++ standard selection. ([src/CMakeLists.txtL5-R6](https://github.com/shimat/opencvsharp/pull/1846/files#diff-148715d6ea0c0ea0a346af3f6bd610d010d490eca35ac6a9b408748f7ca9e3f4L5-R6))

Documentation and Formatting

  • Improved .github/copilot-instructions.md with clearer sectioning, agent mode command guidelines, and minor formatting fixes. ([[1]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-227c2c26cb2ee0ce0f46a320fc48fbcbdf21801a57f59161b1d0861e8aad55f5L1-R3), [[2]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-227c2c26cb2ee0ce0f46a320fc48fbcbdf21801a57f59161b1d0861e8aad55f5L28-L29), [[3]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-227c2c26cb2ee0ce0f46a320fc48fbcbdf21801a57f59161b1d0861e8aad55f5L38-L39), [[4]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-227c2c26cb2ee0ce0f46a320fc48fbcbdf21801a57f59161b1d0861e8aad55f5R57-R63))
  • Fixed BOM and formatting issues in several C# files for consistency. ([[1]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-ac43cd03e45794d42e03654eaa1cd04e43df42495ffaac5cb9a91066ddd9de20L1-R1), [[2]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-72c1c0746dcb617e4747e583e17c179f3abbdf8fa2289d419a12b127e5c803fdL1-R1), [[3]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-ad07909e6707b550ef4f5a58c5b62c6d137a49f0833c78542d09698527602d96L1-R1), [[4]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-187dca213f6001ea2729e07815379803e4c954aaea2c39d3df523fe122be2b91L1-R1), [[5]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-77f15e0d6998539a942d14127772e36ae77b195699b2ad3f4fc10b439ccd487cL1-R1), [[6]](https://github.com/shimat/opencvsharp/pull/1846/files#diff-77f15e0d6998539a942d14127772e36ae77b195699b2ad3f4fc10b439ccd487cL3512-R3512))

These changes modernize the codebase's approach to native resource management, reduce ambiguity around pointer ownership, and improve maintainability and safety for future development.

@shimat shimat self-assigned this Mar 29, 2026
@shimat
shimat merged commit 6a9bff2 into main Mar 29, 2026
8 checks passed
@shimat
shimat deleted the safehandle_dev branch March 29, 2026 12:01
shimat added a commit that referenced this pull request May 26, 2026
These classes had no test coverage, which is why the calling-convention
mismatch introduced in #1846 went undetected until now.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
MaiKuraki pushed a commit to MaiKuraki/opencvsharp that referenced this pull request May 29, 2026
…eptionStatus pattern

The migration in shimat#1846 updated the C# P/Invoke declarations for
imgproc_Ptr_LineSegmentDetector_get and imgproc_Ptr_LineSegmentDetector_delete
to use the ExceptionStatus + out-parameter convention, but the corresponding
C++ implementations were left in the old direct-return style, causing a
calling-convention mismatch that resulted in a null RawPtr and subsequent
segfault. Closes shimat#1876.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
MaiKuraki pushed a commit to MaiKuraki/opencvsharp that referenced this pull request May 29, 2026
…s pattern

Same calling-convention mismatch as shimat#1876: photo_Ptr_MergeDebevec_get/delete
and photo_Ptr_MergeMertens_get/delete were left in the old direct-return style
when shimat#1846 updated the C# P/Invoke declarations to ExceptionStatus + out-param.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
MaiKuraki pushed a commit to MaiKuraki/opencvsharp that referenced this pull request May 29, 2026
…eMethods

All feature detector Create() methods (SIFT, BRISK, ORB, MSER,
FastFeatureDetector, AgastFeatureDetector, GFTTDetector, SimpleBlobDetector,
KAZE, AKAZE) use the shared features2d_Ptr_Feature2D_get to retrieve the raw
pointer. The individual Ptr_xxx_get declarations were added during shimat#1846 but
never called, and have no corresponding C++ implementation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@shimat shimat added the bug Confirmed defect in OpenCvSharp label Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed defect in OpenCvSharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant