---
name: lambda-capture-finder
description: Detects lambda capture lifetime issues
---

**Finding ID Prefix:** `LAMBDA` (e.g., LAMBDA-001, LAMBDA-002)

**Bug Patterns to Find:**

1. **Dangling Reference Capture**
   - Capturing local by reference in escaping lambda
   - Lambda stored outlives captured reference
   - Async callback with reference capture

2. **Dangling this Capture**
   - [this] or [=] in lambda outliving object
   - Lambda stored in callback then object destroyed
   - Capturing this in detached thread

3. **Capture-by-Value Issues**
   - Large object captured by value unnecessarily
   - Mutable lambda modifying copy not original
   - Reference wrapper captured by value

4. **Init-Capture Issues**
   - Init-capture with dangling reference
   - Move-capture then use original
   - Init-capture evaluation order

5. **Generic Lambda Issues**
   - auto&& parameter with unexpected lifetime
   - Perfect forwarding in generic lambda

**Common False Positives to Avoid:**

- **Lambda immediately invoked:** IIFE doesn't outlive captures
- **Lambda never escapes:** If lambda doesn't escape scope, references are safe
- **Shared ownership:** shared_ptr captured keeps object alive
- **Copy intended:** Large capture by value may be intentional for thread safety
- **Synchronous callback:** If callback is called and returns before function exits

**Search Patterns:**
```
\[\s*&\s*\]|\[\s*=\s*\]|\[\s*this\s*\]
\[\s*&\w+|\[\s*\w+\s*=
std::function.*=.*\[
std::thread.*\[|async.*\[|detach.*\[
callback.*\[|handler.*\[
```
