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.

Exact message you may see object backing the pointer will be destroyed at the end of the full-expressiondangling string_viewtemporary string_view dangling
Seen on: C++ Clang GCC

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_view bound to the result of a function returning std::string
  • c_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

  1. Store the owning std::string and take the view from it
  2. Return std::string by value
  3. Run AddressSanitizer with detect_stack_use_after_scope=1

Detailed fix by platform

Clang

  1. -Wdangling -Wdangling-gsl; GCC 13: -Wdangling-pointer -Wdangling-reference

Code examples

view of a temporary

cpp
std::string make();
std::string_view bad = make();            // dangling after this line
std::string owner = make();
std::string_view good = owner;            // owner outlives view

How to diagnose

  1. Owner — Who owns the characters?
  2. Lifetime — Does the view outlive it?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

address of local variable returned — C/C++: warning: address of local variable 'x' returned [-Wreturn-local-addr] (C/C++)address of local vari…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…iterator invalidation — Undefined behaviour: using an invalidated iterator after push_back / erase on std::vector (C/C++)iterator invalidationreference to is ambiguous — C++: error: reference to 'X' is ambiguous (using namespace std conflicts) (C/C++)reference to is ambig…region FLASH overflowed — arm-none-eabi-ld: region `FLASH' overflowed by N bytes (C/C++)region FLASH overflow…dangling

Explore the map of this error Full error map →

🧠 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.