Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

False memory leaks in a MFC project

I have an MFC project, that is linking to a third party dll. At program exit, the IDE reports back that "Detected memory leaks!" and dumps the leaks.

These leaks are from the third party dll. I'm pretty sure that these are falsely being reported. (A quick Google check states that MFC checks for memory leaks, before the CRT destroys variables at the file scope.)

Is there a way to disable the memory leak check, only for that dll?

In response to the answer posted so far

I don't think I'm misusing the API. The behavior I suspect can be reproduced with a simple project

  1. Using VS2005
  2. Create a new solution.
  3. Create a new project (MFC application.)
  4. Create a new project (Windows 32 Project, Application Type: DLL, Export Symbols)
  5. In the dll project, export a function.
  6. In the dll project, define class Foo as follows

Here's the code

class foo
{
    public:
        foo(void)  { p = new int; };
        ~foo(void) { delete p; }
    private:
        int* p;
};

In the dll project, create an instance of the class foo, scoped at the file level.

foo g_foo;

// This is an example of an exported function.
TEMPDLL_API int exportedFunction(void)
{
    return 42;
}

In the MFC project, link to the library, and call the exported function in InitInstance()

Running the application, will report a memory leak, even though p is deleted, when the destructor for g_foo is called.

like image 462
user75810 Avatar asked Dec 22 '22 12:12

user75810


1 Answers

Found what I wanted.

  • In the project property dialogs, under Linker | Input
  • Specify the dll as a Delay Loaded DLL.

Once I did this, Visual Studio no longer reported any memory leaks.

like image 61
user75810 Avatar answered Apr 19 '23 20:04

user75810