Repository navigation
Migrate native pointer management to SafeHandle and fix related issues - #1846
Merged
Merged
Conversation
# Conflicts: # .github/copilot-instructions.md # src/OpenCvSharp/Modules/core/Mat/Mat.cs # src/OpenCvSharp/Modules/objdetect/FaceDetectorYN.cs
# Conflicts: # .github/copilot-instructions.md
Migrate native pointer management to SafeHandle
This was referenced May 26, 2026
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>
This was referenced Jun 22, 2026
This was referenced Jul 6, 2026
This was referenced Jul 25, 2026
This was referenced Aug 10, 2026
This was referenced Sep 9, 2026
Merged
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.
This pull request introduces significant refactoring to the OpenCvSharp resource management model, primarily by removing the legacy
DisposableCvObjectandICvPtrHolderpatterns and replacing them with two new base classes:CvObjectandCvPtrObject. 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
DisposableCvObjectandICvPtrHolder, replacing them with a newCvObjectbase class that usesOpenCvSafeHandlefor 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))CvPtrObjectas 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))Cv2to use the new base classes, introducingToPtr(CvObject?)andToPtr(CvPtrObject?). ([src/OpenCvSharp/Cv2/Cv2.csL30-R33](https://github.com/shimat/opencvsharp/pull/1846/files#diff-25db53258b4a54b4c23f03a357d6646c03a149e32fab5d33b3bbbd014aa7aa18L30-R33))MatSafeHandlefor safe management ofcv::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
System.Drawing.CommonandMicrosoft.SourceLink.GitHubin 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))build_opencv_windows.ps1) to handle Visual Studio detection more robustly, ensure UTF-8 output, and always remove staleCMakeCache.txtbefore 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))[src/CMakeLists.txtL5-R6](https://github.com/shimat/opencvsharp/pull/1846/files#diff-148715d6ea0c0ea0a346af3f6bd610d010d490eca35ac6a9b408748f7ca9e3f4L5-R6))Documentation and Formatting
.github/copilot-instructions.mdwith 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))[[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.