如何释放从函数返回的垫子?
How to release a Mat that's being returned from a function?
如果在一个函数中我正在创建一个新的 Mat 然后我 return 那个 Mat,那个 Mat 对象什么时候被释放?
假设我有这个示例函数:
Mat sampleFunction(Mat frameHSV) {
Mat filtered2 = new Mat(); // create a new Mat object
Mat frameRGB = new Mat(); // create a new Mat object
Imgproc.cvtColor(frameHSV, frameRGB, Imgproc.COLOR_HSV2RGB); // convert to RGB
Core.subtract(frameRGB, frameHSV, filtered2); // subtract Mats, just a made up operation here
frameRGB.release(); // release the RGB mat to clear up memory
return filtered2;
}
所以我传入了 frameHSV,然后我在示例 Function-filtered2 和 frameRGB 中创建了两个 Mats。 FrameRGB 在函数结束时释放,但 Filtered2 已 returned,因此从未释放。
如何释放 filtered2?或者当我 return 垫子时自动释放?
愚蠢的问题,但谢谢 Selvin 的快速回答。
正如 Selvin 所说,只需释放正在调用的结果
result = sampleFunction(frameHSV)
result.release() // releases the Mat that was returned from the function
如果我错了请纠正我,但 Mat
应该会在函数调用完成后自动释放。由于变量 filtered2
的生命周期在函数调用后结束。
如果在一个函数中我正在创建一个新的 Mat 然后我 return 那个 Mat,那个 Mat 对象什么时候被释放?
假设我有这个示例函数:
Mat sampleFunction(Mat frameHSV) {
Mat filtered2 = new Mat(); // create a new Mat object
Mat frameRGB = new Mat(); // create a new Mat object
Imgproc.cvtColor(frameHSV, frameRGB, Imgproc.COLOR_HSV2RGB); // convert to RGB
Core.subtract(frameRGB, frameHSV, filtered2); // subtract Mats, just a made up operation here
frameRGB.release(); // release the RGB mat to clear up memory
return filtered2;
}
所以我传入了 frameHSV,然后我在示例 Function-filtered2 和 frameRGB 中创建了两个 Mats。 FrameRGB 在函数结束时释放,但 Filtered2 已 returned,因此从未释放。
如何释放 filtered2?或者当我 return 垫子时自动释放?
愚蠢的问题,但谢谢 Selvin 的快速回答。
正如 Selvin 所说,只需释放正在调用的结果
result = sampleFunction(frameHSV)
result.release() // releases the Mat that was returned from the function
如果我错了请纠正我,但 Mat
应该会在函数调用完成后自动释放。由于变量 filtered2
的生命周期在函数调用后结束。