This query finds return statements that return pointers to an object allocated on the stack. The lifetime of a stack allocated memory location only lasts until the function returns, and the contents of that memory become undefined after that. Clearly, using a pointer to stack memory after the function has already returned will have undefined results.
This check is an approximation, so some results may not be actual defects in the program. It is not possible in general to compute the values of pointers without running the program with all input data.
Do not return pointers to stack memory locations. Instead, create an output parameter, or create a heap-allocated buffer. You can then copy the contents of the stack-allocated memory to the heap-allocated buffer and return that location instead.
The example below the reference to
myRecord is useful only while the containing function is running.
If you need to access the object outside this function, either create an output parameter with its value, or copy the object into