是什么导致我的OpenCV内存泄漏 [英] What is causing my memory leak with OpenCV

查看:91
本文介绍了是什么导致我的OpenCV内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement OpenCV into my application but every time I call a function there is a memory leak. I guess it has something to do with how I have used the library with Visual Studio but I tested it with a blank project and it seemed to work fine with the same settings.

我要实现的代码:

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;
}

如果我实现一些简单的事情,

If I implement something as simple as:

cv::Mat aVar;

在我放置此处输入代码!"的空间中Visual Studio表示一旦程序终止,就会发生内存泄漏.任何想法可能是什么问题?

in the space where I have put "CODE TO GO HERE!" Visual Studio says there is a memory leak once the program has terminated. Any ideas what the problem could be?

推荐答案

就像我在上一篇文章中所说的那样,细节很重要.非MFC dll会在MFC dll之前加载,并且如果在MFC退出之前没有任何未释放的数据,则MFC会错误地将其报告为内存泄漏.这是一个已知问题,当将opencv与mfc一起使用时会出现问题.解决方案是:

Like I said in your last post, the details are important. The non-MFC dll loads before MFC dll and if there is any data not freed before MFC exits, MFC falsely reports this as a memory leak. This is a known issue which is a problem when using opencv with mfc. The solution is to:

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

  1. Static link MFC library (most common way)

尝试通过变通方法强制首先在上面的链接中加载mfc dll

Try the workaround to force mfc dll to be loaded first in the link above

延迟加载dll,如 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆