gpt4 book ai didi

c++ - 是什么导致我的 OpenCV 内存泄漏

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:17 24 4
gpt4 key购买 nike

我正在尝试将 OpenCV 实现到我的应用程序中,但每次调用函数时都会发生内存泄漏。我想这与我在 Visual Studio 中使用该库的方式有关,但我用一个空白项目对其进行了测试,它似乎在相同的设置下工作正常。

我要实现的代码:

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize Microsoft Foundation Classes, and print an error if failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// Application starts here...

// Time the application's execution time.
TIMER start;

// CODE TO GO HERE!

TIMER end;

TIMER elapsed;

elapsed = end - start;

__int64 ticks_per_second = start.get_frequency();

// Display the resulting time...

double elapsed_seconds = (double)elapsed.get_time() / (double)ticks_per_second;

cout << "Elapsed time (seconds): " << elapsed_seconds;
cout << endl;
cout << "Press a key to continue" << endl;

char c;
cin >> c;
}

return nRetCode;
}

如果我实现像这样简单的东西:

cv::Mat aVar;

在我放置“CODE TO HERE!”的地方Visual Studio 表示程序终止后会发生内存泄漏。知道问题可能是什么吗?

最佳答案

正如我在您上一篇文章中所说,细节很重要。非 MFC dll 在 MFC dll 之前加载,如果在 MFC 退出之前有任何数据未释放,则 MFC 错误地将此报告为内存泄漏。这是 known issue这是将 opencv 与 mfc 一起使用时的问题。解决方案是:

  1. 静态链接MFC库(最常用的方式)

  2. 尝试在上面的链接中强制首先加载 mfc dll 的解决方法

  3. 延迟加载 dll,如 this question 所示.

关于c++ - 是什么导致我的 OpenCV 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16430373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com