V1017. Variable of the 'string_view' type references a temporary object which will be removed after evaluation of an expression.

The analyzer has detected that an instance of class 'std::string_view' is initialized by or assigned a temporary object.

Consider the following example:

std::string hello = "Hello, ";
std::string_view helloWorldPtr = hello + "world\n";
std::cout << helloWorldPtr;

In the second line of this code, a temporary object of the type 'std::string' will be created, the pointer to which will be copied when initializing the instance of the 'std::string_view' class. After that, once the initialization expression has been evaluated, the temporary object will be destroyed; therefore, the pointer used in the third line will be referring to a freed memory block.

This is the fixed version:

std::string hello = "Hello, ";
const std::string helloWorld = hello + "world\n";
std::string_view helloWorldPtr = helloWorld;
std::cout << helloWorldPtr;

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-416.

Bugs Found

Checked Projects
Collected Errors
14 072
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →