Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV: avoiding memory corruption when using Mat constructors taking primitive arrays #1002

Merged
merged 8 commits into from
Feb 1, 2021
46 changes: 23 additions & 23 deletions opencv/src/main/java/org/bytedeco/opencv/presets/opencv_core.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,34 @@ public void map(InfoMap infoMap) {
.put(new Info("cv::Mat").base("AbstractMat"))
.put(new Info("cv::noArray()").javaText("public static Mat noArray() { return null; }"))
.put(new Info("cv::Mat(int, int, int, void*, size_t)").javaText(
"public Mat(int rows, int cols, int type, Pointer data, @Cast(\"size_t\") long step/*=AUTO_STEP*/) { super((Pointer)null); allocate(rows, cols, type, data, step); this.pointer = data; }\n"
"/** JavaCPP warning: the data of the OpenCV Mat returned by this constructor is externally allocated from OpenCV point of view, so will not benefit from OpenCV reference counting feature. The data may be deallocated when the returned Mat is garbage collected, even if another Mat has been created pointing to the same data with, e.g., reshape, rowRange, etc... */ public Mat(int rows, int cols, int type, Pointer data, @Cast(\"size_t\") long step/*=AUTO_STEP*/) { super((Pointer)null); allocate(rows, cols, type, data, step); this.pointer = data; }\n"
+ "private native void allocate(int rows, int cols, int type, Pointer data, @Cast(\"size_t\") long step/*=AUTO_STEP*/);\n"
+ "private Pointer pointer; // a reference to prevent deallocation\n"
+ "public Mat(int rows, int cols, int type, Pointer data) { this(rows, cols, type, data, AUTO_STEP); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */ public Mat(int rows, int cols, int type, Pointer data) { this(rows, cols, type, data, AUTO_STEP); }\n"
+ "public Mat(CvArr arr) { super(cvarrToMat(arr)); this.pointer = arr; }\n"
+ "public Mat(Point points) { this(1, Math.max(1, points.limit() - points.position()), CV_32SC2, points); this.pointer = points; }\n"
+ "public Mat(Point2f points) { this(1, Math.max(1, points.limit() - points.position()), CV_32FC2, points); this.pointer = points; }\n"
+ "public Mat(Point2d points) { this(1, Math.max(1, points.limit() - points.position()), CV_64FC2, points); this.pointer = points; }\n"
+ "public Mat(Point3i points) { this(1, Math.max(1, points.limit() - points.position()), CV_32SC3, points); this.pointer = points; }\n"
+ "public Mat(Point3f points) { this(1, Math.max(1, points.limit() - points.position()), CV_32FC3, points); this.pointer = points; }\n"
+ "public Mat(Point3d points) { this(1, Math.max(1, points.limit() - points.position()), CV_64FC3, points); this.pointer = points; }\n"
+ "public Mat(Scalar scalar) { this(1, Math.max(1, scalar.limit() - scalar.position()), CV_64FC4, scalar); this.pointer = scalar; }\n"
+ "public Mat(Scalar4i scalar) { this(1, Math.max(1, scalar.limit() - scalar.position()), CV_32SC4, scalar); this.pointer = scalar; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point points) { this(1, Math.max(1, points.limit() - points.position()), CV_32SC2, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point2f points) { this(1, Math.max(1, points.limit() - points.position()), CV_32FC2, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point2d points) { this(1, Math.max(1, points.limit() - points.position()), CV_64FC2, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point3i points) { this(1, Math.max(1, points.limit() - points.position()), CV_32SC3, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point3f points) { this(1, Math.max(1, points.limit() - points.position()), CV_32FC3, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Point3d points) { this(1, Math.max(1, points.limit() - points.position()), CV_64FC3, points); this.pointer = points; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Scalar scalar) { this(1, Math.max(1, scalar.limit() - scalar.position()), CV_64FC4, scalar); this.pointer = scalar; }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(Scalar4i scalar) { this(1, Math.max(1, scalar.limit() - scalar.position()), CV_32SC4, scalar); this.pointer = scalar; }\n"
+ "public Mat(byte ... b) { this(b, false); }\n"
+ "public Mat(byte[] b, boolean signed) { this(new BytePointer(b), signed); }\n"
+ "public Mat(byte[] b, boolean signed) { this(1, b.length, signed ? CV_8SC1:CV_8UC1); data().put(b); }\n"
+ "public Mat(short ... s) { this(s, true); }\n"
+ "public Mat(short[] s, boolean signed) { this(new ShortPointer(s), signed); }\n"
+ "public Mat(int ... n) { this(new IntPointer(n)); }\n"
+ "public Mat(double ... d) { this(new DoublePointer(d)); }\n"
+ "public Mat(float ... f) { this(new FloatPointer(f)); }\n"
+ "private Mat(long rows, long cols, int type, Pointer data) { this((int)Math.min(rows, Integer.MAX_VALUE), (int)Math.min(cols, Integer.MAX_VALUE), type, data, AUTO_STEP); }\n"
+ "public Mat(BytePointer p) { this(p, false); }\n"
+ "public Mat(BytePointer p, boolean signed) { this(1, Math.max(1, p.limit() - p.position()), signed ? CV_8SC1 : CV_8UC1, p); }\n"
+ "public Mat(ShortPointer p) { this(p, false); }\n"
+ "public Mat(ShortPointer p, boolean signed) { this(1, Math.max(1, p.limit() - p.position()), signed ? CV_16SC1 : CV_16UC1, p); }\n"
+ "public Mat(IntPointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_32SC1, p); }\n"
+ "public Mat(FloatPointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_32FC1, p); }\n"
+ "public Mat(DoublePointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_64FC1, p); }\n"))
+ "public Mat(short[] s, boolean signed) { this(1, s.length, signed ? CV_16SC1:CV_16UC1); ((ShortBuffer)createBuffer()).put(s); }\n"
+ "public Mat(int ... n) { this(1, n.length, CV_32SC1); ((IntBuffer)createBuffer()).put(n); }\n"
+ "public Mat(double ... d) { this(1, d.length, CV_64FC1); ((DoubleBuffer)createBuffer()).put(d); }\n"
+ "public Mat(float ... f) { this(1, f.length, CV_32FC1); ((FloatBuffer)createBuffer()).put(f); }\n"
HGuillemet marked this conversation as resolved.
Show resolved Hide resolved
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */private Mat(long rows, long cols, int type, Pointer data) { this((int)Math.min(rows, Integer.MAX_VALUE), (int)Math.min(cols, Integer.MAX_VALUE), type, data, AUTO_STEP); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(BytePointer p) { this(p, false); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(BytePointer p, boolean signed) { this(1, Math.max(1, p.limit() - p.position()), signed ? CV_8SC1 : CV_8UC1, p); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(ShortPointer p) { this(p, false); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(ShortPointer p, boolean signed) { this(1, Math.max(1, p.limit() - p.position()), signed ? CV_16SC1 : CV_16UC1, p); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(IntPointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_32SC1, p); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(FloatPointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_32FC1, p); }\n"
+ "/** See the warning about {@link #Mat(int, int, int, Pointer, long)}. */public Mat(DoublePointer p) { this(1, Math.max(1, p.limit() - p.position()), CV_64FC1, p); }\n"))
.put(new Info("cv::Mat::zeros(int, int*, int)", "cv::Mat::ones(int, int*, int)").skip())
.put(new Info("cv::Mat::size").javaText("public native @ByVal Size size();\n@MemberGetter public native int size(int i);"))
.put(new Info("cv::Mat::step").javaText("@MemberGetter public native long step();\n@MemberGetter public native long step(int i);"))
Expand Down