Skip to content

Commit e27b400

Browse files
authored
Merge pull request #1998 from shimat/feature/internal-safehandle-null-1938
Internal: remove dead KeepAlive/ElemPtr, add OpenCvSafeHandle.Null for optional args
2 parents cacda4f + 220755d commit e27b400

19 files changed

Lines changed: 52 additions & 57 deletions

File tree

src/OpenCvSharp/Fundamentals/OpenCvSafeHandle.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ protected OpenCvSafeHandle(IntPtr existingHandle, bool ownsHandle)
4444

4545
/// <inheritdoc />
4646
public override bool IsInvalid => handle == IntPtr.Zero;
47+
48+
/// <summary>
49+
/// A non-owning handle wrapping a null native pointer. Pass this for an optional
50+
/// SafeHandle-typed P/Invoke argument (e.g. a mask) when the caller has none, instead
51+
/// of overloading the parameter type with a plain <see cref="IntPtr"/>.
52+
/// </summary>
53+
public static OpenCvSafeHandle Null { get; } = new OpenCvPtrSafeHandle(IntPtr.Zero, ownsHandle: false, releaseAction: null);
4754
}

src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_line_descriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static partial ExceptionStatus line_descriptor_LSDDetector_new2(
2929

3030
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
3131
public static partial ExceptionStatus line_descriptor_LSDDetector_detect1(
32-
OpenCvSafeHandle obj, IntPtr image, IntPtr keypoints, int scale, int numOctaves, IntPtr mask);
32+
OpenCvSafeHandle obj, IntPtr image, IntPtr keypoints, int scale, int numOctaves, OpenCvSafeHandle mask);
3333

3434
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
3535
public static partial ExceptionStatus line_descriptor_LSDDetector_detect2(

src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_stdvector.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ static partial class NativeMethods
282282
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
283283
public static partial nuint vector_Mat_getSize(OpenCvSafeHandle vector);
284284
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
285-
public static partial IntPtr vector_Mat_getPointer(OpenCvSafeHandle vector);
286-
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
287285
public static partial void vector_Mat_delete(IntPtr vector);
288286
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
289287
public static partial void vector_Mat_assignToArray(OpenCvSafeHandle vector, [MarshalAs(UnmanagedType.LPArray)] IntPtr[] arr);

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_FileNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public static partial ExceptionStatus core_FileNode_readRaw(
8080
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
8181
public static partial ExceptionStatus core_FileNode_read_String(OpenCvSafeHandle node, IntPtr value, [MarshalAs(UnmanagedType.LPStr)] string? defaultValue);
8282
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
83-
public static partial ExceptionStatus core_FileNode_read_Mat(OpenCvSafeHandle node, IntPtr mat, IntPtr defaultMat);
83+
public static partial ExceptionStatus core_FileNode_read_Mat(OpenCvSafeHandle node, IntPtr mat, OpenCvSafeHandle defaultMat);
8484
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
85-
public static partial ExceptionStatus core_FileNode_read_SparseMat(OpenCvSafeHandle node, IntPtr mat, IntPtr defaultMat);
85+
public static partial ExceptionStatus core_FileNode_read_SparseMat(OpenCvSafeHandle node, IntPtr mat, OpenCvSafeHandle defaultMat);
8686
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
8787
public static partial ExceptionStatus core_FileNode_read_vectorOfKeyPoint(OpenCvSafeHandle node, IntPtr keypoints);
8888
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_Mat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public static partial ExceptionStatus core_Mat_ones_MatShape(
107107
public static partial ExceptionStatus core_Mat_assignTo(OpenCvSafeHandle self, IntPtr m, int type);
108108

109109
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
110-
public static partial ExceptionStatus core_Mat_setTo_Scalar(OpenCvSafeHandle self, Scalar value, IntPtr mask);
110+
public static partial ExceptionStatus core_Mat_setTo_Scalar(OpenCvSafeHandle self, Scalar value, OpenCvSafeHandle mask);
111111
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
112-
internal static partial ExceptionStatus core_Mat_setTo_InputArray(OpenCvSafeHandle self, in InputArrayProxy value, IntPtr mask);
112+
internal static partial ExceptionStatus core_Mat_setTo_InputArray(OpenCvSafeHandle self, in InputArrayProxy value, OpenCvSafeHandle mask);
113113

114114
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
115115
public static partial ExceptionStatus core_Mat_reshape1(

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_UMat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ static partial class NativeMethods
8484
public static partial ExceptionStatus core_UMat_assignTo(OpenCvSafeHandle self, IntPtr m, int type);
8585

8686
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
87-
public static partial ExceptionStatus core_UMat_setTo_Scalar(OpenCvSafeHandle self, Scalar value, IntPtr mask);
87+
public static partial ExceptionStatus core_UMat_setTo_Scalar(OpenCvSafeHandle self, Scalar value, OpenCvSafeHandle mask);
8888
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
89-
internal static partial ExceptionStatus core_UMat_setTo_InputArray(OpenCvSafeHandle self, in InputArrayProxy value, IntPtr mask);
89+
internal static partial ExceptionStatus core_UMat_setTo_InputArray(OpenCvSafeHandle self, in InputArrayProxy value, OpenCvSafeHandle mask);
9090

9191
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
9292
public static partial ExceptionStatus core_UMat_reshape1(

src/OpenCvSharp/Internal/PInvoke/NativeMethods/stitching/NativeMethods_stitching_Matchers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static partial ExceptionStatus stitching_FeaturesMatcher_apply(
4545
public static partial ExceptionStatus stitching_FeaturesMatcher_apply2(
4646
OpenCvSafeHandle obj,
4747
WImageFeatures[] features, int featuresSize,
48-
IntPtr mask,
48+
OpenCvSafeHandle mask,
4949
IntPtr outSrcImgIdx,
5050
IntPtr outDstImgIdx,
5151
IntPtr outMatches,

src/OpenCvSharp/Internal/PInvoke/NativeMethods/video/NativeMethods_video_tracking.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static partial ExceptionStatus video_KalmanFilter_init(OpenCvSafeHandle o
7979
public static partial ExceptionStatus video_KalmanFilter_delete(IntPtr obj);
8080

8181
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
82-
public static partial ExceptionStatus video_KalmanFilter_predict(OpenCvSafeHandle obj, IntPtr control, out IntPtr returnValue);
82+
public static partial ExceptionStatus video_KalmanFilter_predict(OpenCvSafeHandle obj, OpenCvSafeHandle control, out IntPtr returnValue);
8383

8484
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
8585
public static partial ExceptionStatus video_KalmanFilter_correct(OpenCvSafeHandle obj, IntPtr measurement, out IntPtr returnValue);

src/OpenCvSharp/Internal/Vectors/VectorOfMat.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,6 @@ public int Size
7272
}
7373
}
7474

75-
/// <summary>
76-
/// &amp;vector[0]
77-
/// </summary>
78-
public IntPtr ElemPtr
79-
{
80-
get
81-
{
82-
var res = NativeMethods.vector_Mat_getPointer(Handle);
83-
// Returns an interior pointer into this vector; keep it alive for the caller's dereference.
84-
GC.KeepAlive(this);
85-
return res;
86-
}
87-
}
88-
8975
/// <summary>
9076
/// Converts std::vector to managed array
9177
/// </summary>

src/OpenCvSharp/Modules/core/FileNode.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ public Mat ReadMat(Mat? defaultMat = null)
512512
try
513513
{
514514
NativeMethods.HandleException(
515-
NativeMethods.core_FileNode_read_Mat(Handle, value.CvPtr, Cv2.ToPtr(defaultMat)));
516-
GC.KeepAlive(defaultMat);
515+
NativeMethods.core_FileNode_read_Mat(Handle, value.CvPtr, defaultMat?.Handle ?? OpenCvSafeHandle.Null));
517516
}
518517
catch
519518
{
@@ -534,8 +533,7 @@ public SparseMat ReadSparseMat(SparseMat? defaultMat = null)
534533
try
535534
{
536535
NativeMethods.HandleException(
537-
NativeMethods.core_FileNode_read_SparseMat(Handle, value.CvPtr, Cv2.ToPtr(defaultMat)));
538-
GC.KeepAlive(defaultMat);
536+
NativeMethods.core_FileNode_read_SparseMat(Handle, value.CvPtr, defaultMat?.Handle ?? OpenCvSafeHandle.Null));
539537
}
540538
catch
541539
{

0 commit comments

Comments
 (0)