warning: object backing the pointer will be destroyed at the end of the full-expression (std::string_view from a temporary)
A string_view, span or pointer refers to a temporary that is destroyed before it is used.
object backing the pointer will be destroyed at the end of the full-expressiondangling string_viewtemporary string_view dangling Meaning
std::string_view sv = make_string(); leaves sv pointing into freed memory. Newer compilers warn (-Wdangling-gsl), but without a warning it simply reads garbage.
Common causes
string_viewbound to the result of a function returningstd::stringc_str()of a temporary stored- View returned from a function from a local string
- Lambda capturing a view of a short-lived object
⚡ Quick fix
- Store the owning
std::stringand take the view from it - Return
std::stringby value - Run AddressSanitizer with
detect_stack_use_after_scope=1
Detailed fix by platform
Clang
-Wdangling -Wdangling-gsl; GCC 13:-Wdangling-pointer -Wdangling-reference
Code examples
view of a temporary
std::string make();
std::string_view bad = make(); // dangling after this line
std::string owner = make();
std::string_view good = owner; // owner outlives viewHow to diagnose
- Owner — Who owns the characters?
- Lifetime — Does the view outlive it?
- ASan — stack-use-after-scope?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- address of local variable returned C/C++: warning: address of local variable 'x' returned [-Wreturn-local-addr]
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
Most viewed in C/C++
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- was not declared in this scope C++: error: 'x' was not declared in this scope (undeclared identifier)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 2 Dec 2026