Share via


C6014

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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.

Example

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

Example

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

C6211