Warning C6014

Leaking memory 'pointer-name'.

This warning indicates that the specified pointer points to allocated memory or some other allocated resource that hasn't been freed.

Remarks

The analyzer checks for this condition only when the _Analysis_mode_(_Analysis_local_leak_checks_) SAL annotation is specified. By default, this annotation is specified for Windows kernel mode (driver) code. For more information about SAL annotations, see Using SAL Annotations to Reduce C/C++ Code Defects.

This warning is reported for both memory and resource leaks when the resource is commonly aliased to another location. Memory is aliased when a pointer to the memory escapes the function by using an _Out_ parameter annotation, global variable, or return value. This warning can be reported on function exit if the argument is annotated that its release is expected.

Code Analysis won't recognize the actual implementation of a memory allocator (involving address arithmetic) and won't recognize that memory is allocated (although many wrappers will be recognized). In this case, the analyzer doesn't recognize that the memory was allocated and issues this warning. To suppress the false positive, use a #pragma warning(disable: 6014) directive on the line that precedes the opening brace { of the function body.

Code analysis name: MEMORY_LEAK

Examples

The following code generates warning C6014:

// cl.exe /analyze /EHsc /nologo /W4
#include <sal.h>
#include <stdlib.h>
#include <string.h>

_Analysis_mode_(_Analysis_local_leak_checks_)

#define ARRAYSIZE 10
const int TEST_DATA [ARRAYSIZE] = {10,20,30,40,50,60,70,80,90,100};

void f( )
{
    int *p = (int *)malloc(sizeof(int)*ARRAYSIZE);
    if (p) {
        memcpy(p, TEST_DATA, sizeof(int)*ARRAYSIZE);
        // code ...
    }
}

int main( )
{
    f();
}

The following code corrects the warning by releasing the memory:

// cl.exe /analyze /EHsc /nologo /W4
#include <sal.h>
#include <stdlib.h>
#include <string.h>

_Analysis_mode_(_Analysis_local_leak_checks_)

#define ARRAYSIZE 10
const int TEST_DATA [ARRAYSIZE] = {10,20,30,40,50,60,70,80,90,100};

void f( )
{
    int *p = (int *)malloc(sizeof(int)*ARRAYSIZE);
    if (p) {
        memcpy(p, TEST_DATA, sizeof(int)*ARRAYSIZE);
        // code ...
        free(p);
    }
}

int main( )
{
    f();
}

To avoid these kinds of potential leaks altogether, use the mechanisms that are provided by the C++ Standard Library (STL). These include shared_ptr, unique_ptr, and containers such as vector. For more information, see Smart pointers and C++ Standard Library.

// cl.exe /analyze /EHsc /nologo /W4
#include <sal.h>
#include <memory>

using namespace std;

_Analysis_mode_(_Analysis_local_leak_checks_)

const int ARRAYSIZE = 10;
const int TEST_DATA [ARRAYSIZE] = {10,20,30,40,50,60,70,80,90,100};

void f( )
{
    unique_ptr<int[]> p(new int[ARRAYSIZE]);
    std::copy(begin(TEST_DATA), end(TEST_DATA), p.get());

    // code ...

    // No need for free/delete; unique_ptr
    // cleans up when out of scope.
}

int main( )
{
    f();
}

See also

Warning C6211