Immediately Invoked Coroutine Lambdas in C++23 - Lifetime Pitfalls & Best Practices
summary
Shows a crash scenario when an immediately invoked coroutine lambda captures an awaitable, moves only a pointer into the coroutine frame, and later resumes on another thread causing a dangling capture and undefined behavior in a thread pool queue flow. Demonstrates the concrete fix using C++23 this auto to pass captures by value so the coroutine frame owns them, and a macro-backed lambda invoker pattern that creates an inline coroutine scope; the examples compile and prevent the described lifetime bugs.
AI-written summary · about
chapters (10)
- 0:00Introduction to Coroutine Lambdas
- 0:15Why Use Coroutines in C++
- 0:30The Problem with Awaitables and Handles
- 0:58Building an Immediately Invoked Coroutine Lambda
- 1:24Lifetime Issues and Crashes
- 2:01Compiler Lowering and Dangling Captures
- 2:46The Solution: Pass by Value with Deducing This
- 3:23Using 'this auto' in Lambdas
- 3:54Cleaner Syntax with Lambda Invoker and Macros
- 4:47Final Advice and Best Practices








