C28197

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 C28197: Possibly leaking memory

This warning is reported for both memory and resource leaks when the resource is potentially aliased to another location.

The pointer points to allocated memory or to another allocated resource that was not explicitly freed. This warning is usually due to inadequate annotations on the called function, although inadequate annotations on the calling function can also make this more likely.

This warning can be reported on function exit if an input argument has a __drv_freesMem or __drv_aliasesMem annotation. This warning typically indicates either a valid leak or that a function called by the current function needs additional annotation.

In particular, the absence of the basic _In_ and _Out_ annotations make this warning fairly likely, although the __drv_aliasesMem and __drv_freesMem annotations might be needed as well. A false positive is a likely result of a missing _In_ annotation.

Functions that take a pointer and alias it (thus avoiding a leak) should be annotated with __drv_aliasesMem. If you create a function that inserts an object into a global structure, or passes it to a system function that does that, you should add the __drv_aliasesMem annotation.

Functions that free memory should be annotated with __drv_freesMem. The major functions that free memory already have this annotation.

Example

The following code example generates this warning:

char *p = (char *)malloc(10);  
test(p);   // does not save a copy of p  

The following code example avoids this warning:

char *p = (char *)malloc(10);  
test(p);   // does not save a copy of p  
free(p);