核心函数为anync_res
async_res[i] = std::async(std::launch::async,
this, image, &peak_value, &res, &scaleselect, i->int
{
res[i] = detect(_tmpl, getFeatures(image, 0, scaleselect[i]), peak_value[i]);
return i;
});
其中调用的detect函数,在单线程逐个跑的时候,没有内存泄露问题。采用如上方式调用,会出现内存一直增长的问题。
async_res()函数的上下文如下:
...
else
{
float scaleselect[3] = { 1.0f, 1.0f / scale_step, scale_step };
float peak_value[3];
cv::Point2f res[3];
std::vector<std::future<int>> async_res(3);
for (int i = 0; i < 3; i++)
{
async_res[i] = std::async(std::launch::async,
[this, image, &peak_value, &res, &scaleselect, i]()->int
{
res[i] = detect(_tmpl, getFeatures(image, 0, scaleselect[i]), peak_value[i]);
return i;
});
}
float max_peak_value = -1;
cv::Point2f bestres;
for (int i = 0; i < 3; i++)
{
async_res[i].wait();
if (scale_weight*peak_value[i] > max_peak_value)
{
max_peak_value = peak_value[i];
bestres = res[i];
_scale = _scale*scaleselect[i];
_roi.width = _roi.width*scaleselect[i];
_roi.height = _roi.height*scaleselect[i];
}
}
内存定位的地方肯定是没问题。但是原因由于水平有限,查不到根本原因。希望有大神可以指点一二,或者详细说明一下排查思路。非常感谢!