Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

C6014

warning C6014: Leaking memory.

This warning indicates that the specified pointer points to allocated memory or some other allocated resource that has not been freed. 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.

The following code generates this warning:

// 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();
}

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 means of an _Out_ parameter annotation, global variable, or return value. This warning can be reported on function exit if the argument is annotated as having been expected to be released.

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

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

// 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();
}
 
Show:
© 2014 Microsoft. All rights reserved.