C28198

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at C28198.

warning C28198: Possibly leaking memory due to an exception.

This warning indicates that allocated memory is not being freed after an exception is raised. The statement at the end of the path can raise an exception. The memory was passed to a function that might have saved a copy to be freed later.

This warning is very similar to warning C28197. The annotations that are recommended for use with warning C28197 can also be used here.

The following code example generates this warning:

char *p1 = new char[10];  
char *p2 = new char[10];  
  
test(p1);   // does not save a copy of p  
  
delete[] p2;  
delete[] p1;  

The following code example avoids this warning:

char *p1 = new char[10];  
char *p2 = NULL;  
  
test(p1);   // does not save a copy of p  
try {  
    p2 = new char[10];  
} catch (std::bad_alloc *e) {  
    // just handle the throw  
    ;  
}  

Show: