【发布时间】:2017-01-02 17:48:01
【问题描述】:
我的代码结构如下:
[code 1]
cout << some_string << endl
cout << some_string << endl
cout << some_string << endl
cout << some_string << endl
cout << some_string << endl
[code 2]
当我删除 cout 语句时,代码 2 在存在 cout 语句时给出不同的输出。这是否表明存在内存泄漏?
具体例子来自OPENCV的stitching_detailed.cpp。我有以下代码块:
// code 1 - edit added cameras initialization
vector<CameraParams> cameras(0);
HomographyBasedEstimator estimator;
theRNG().state = 0;
estimator(features, pairwise_matches, cameras);
for (size_t i = 0; i < cameras.size(); ++i)
{
theRNG().state = 0;
Mat R;
cameras[i].R.convertTo(R, CV_32F);
cameras[i].R = R;
LOGLN("Initial intrinsics #" << indices[i]+1 << ":\n" << cameras[i].K());
}
// cout statements
for (size_t i = 0; i < cameras.size(); ++i)
{
cout << cameras[i].aspect << endl;
cout << cameras[i].focal << endl;
cout << cameras[i].K() << endl;
cout << cameras[i].ppx << endl;
cout << cameras[i].ppy << endl;
cout << cameras[i].R << endl;
cout << cameras[i].t << endl;
}
// code 2
theRNG().state = 0;
Ptr<detail::BundleAdjusterBase> adjuster;
if (ba_cost_func == "reproj") adjuster = new detail::BundleAdjusterReproj();
else if (ba_cost_func == "ray") adjuster = new detail::BundleAdjusterRay();
else
{
cout << "Unknown bundle adjustment cost function: '" << ba_cost_func << "'.\n";
return -1;
}
theRNG().state = 0;
Mat_<uchar> refine_mask = Mat::zeros(3, 3, CV_8U);
if (ba_refine_mask[0] == 'x') refine_mask(0,0) = 1;
if (ba_refine_mask[1] == 'x') refine_mask(0,1) = 1;
if (ba_refine_mask[2] == 'x') refine_mask(0,2) = 1;
if (ba_refine_mask[3] == 'x') refine_mask(1,1) = 1;
if (ba_refine_mask[4] == 'x') refine_mask(1,2) = 1;
adjuster->setRefinementMask(refine_mask);
adjuster->setConfThresh(conf_thresh);
(*adjuster)(features, pairwise_matches, cameras);
如果我删除 cout 语句,cameras[x].focal 是 NaN。如果我保留cout 语句,则cameras[x].focal 具有有效值。
我知道它的付出并不多,但我正在努力调试它。任何调试问题的帮助表示赞赏。即使您知道调试它的标准方法,也请告诉我。
【问题讨论】:
-
Is this an indication of some memory leak?不,undefined behavior. -
您是否使用过调试器来逐行查看变量?
-
@Torbjörn 我正在为这个 C++ 代码使用 CLI/C++ 接口。在 Visual Studio 中调试 C#、C++/CLI、C++ 代码太难了。简而言之,我无法通过它。
-
@ubaabd 在 Visual Studio 中调试 C#、C++/CLI、C++ 代码太难了。 -- 我不知道你做错了什么,但这不是真的。
-
@ubaabd -- 你要么 1) 没有将 C# 项目中的 "Debug" 选项设置为 "Native Debugging",要么 2) 你的 DLL 没有加载调试符号,或者 3 ) 你认为你正在调试的 C++ DLL 不是正在加载的那个。
标签: c++ opencv memory-leaks