TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24774 15
to each client's send buffer via ZeroCopyOutputStream → N serializations
* Optimized: serialize once to a lightweight buffer, then memcpy to each client's send buffer

​

// Before: O(N) serializations
for (auto& client : clients) {
client->Send(message); // serializes each time
}

// After: O(1) serialization + O(N) memcpy
LightWeightSendStream stream;
message.SerializeToZeroCopyStream(&stream);
for (auto& client : clients) {
client->Send(stream); // fast memcpy only
}

# 7. Debugging memory corruption with hardware breakpoints

We had a bug where a 192-byte lambda capture was being corrupted at byte offset 136 — intermittently, in production only. Traditional debugging was useless.

Our approach:

1. **Canary message:** Send a dummy message of the same size before the real one. The dummy gets corrupted instead, and we detect it with a known byte pattern.
2. **Hardware breakpoints:** Use CPU debug registers (DR0–DR7) to trigger on write access to the predicted corruption address at runtime.

​

CONTEXT context;
context.Dr0 = (DWORD_PTR)(target_address); // address to watch
context.Dr7 |= (1 << 0); // enable Dr0
context.Dr7 |= (1 << 16); // write access
context.Dr7 |= (3 << 18); // 4 bytes
SetThreadContext(thread_handle, &context);

Combined with `AddVectoredExceptionHandler` and `CaptureStackBackTrace`, this lets us catch the exact call stack that writes to a specific memory address — without attaching a debugger.

**Limitation:** Hardware breakpoints are per-thread (set via thread context), so you only catch corruption if the writing thread is the one with the breakpoint installed.

# 8. Async stack traces for crash dump analysis

With deeply nested async calls (Post → Promise → Task), traditional call stacks are useless — you just see the event loop scheduler. We implemented [async stack traces inspired by Folly](https://developers.facebook.com/blog/post/2021/09/16/async-stack-traces-folly-Introduction/) and Visual Studio Natvis visualizations to reconstruct the logical async call chain in crash dumps.

For unhandled exceptions in coroutines, we store the source location in a static variable so crash dumps always show where the coroutine was last suspended — bridging the gap between the physical stack (scheduler) and the logical stack (your actual code).

# 9. Modern C++ features we adopted (and pre-implemented)

We use C++20 fully (Concepts, Ranges, Coroutines — but not Modules yet, VS support was insufficient at launch). We also pre-implemented several C++23/26 features:

* `std::expected` (C++23) — error handling without exceptions
* `std::ranges::to`, `enumerate`, `zip`, `cartesian_product` (C++23)
* `std::function_ref` (C++26) — lightweight non-owning callable wrapper, zero allocation
* `std::ranges::concat_view` (C++26)

We tried to adopt C++20 Modules in early 2023 but had to stop due to incomplete VS implementation. We're preparing to retry now with VS 2022 updates.

We're excited about C++26's Compile-time Reflection, `hazard_pointer`/RCU, Execution control library, and Contracts.

Happy to answer questions about any of these topics. After 2 years of live service with millions of players, the biggest takeaway is: **the bugs that matter in production are almost never the ones you test for.**

https://redd.it/1qzzdjf
@r_cpp
Meta for Developers Async stack traces in folly: Introduction Facebook For Developers
More from @r_cpp
  1. Oct 7, 2026(Imperfect) Emulation of Rust's borrow checker in C++23 with Clang Extensions https://comp…
  2. Oct 7, 2026Escaping `CreateProcess()` arguments on Windows https://holyblackcat.github.io/blog/2026/0…
  3. Oct 7, 2026flat_map internals https://quantdev.blog/posts/flat-map-internals/index.html https://redd.…
  4. Oct 7, 2026New C++ Conference Videos Released This Month - September 2026 C++Now 2026-08-31 - 2026-09…
  5. Oct 7, 2026Jean Frantz René: C++ Memory safety for safety-critical and non-safety critical applicatio…
  6. Oct 7, 2026Full Meeting C++ 2026 program released https://meetingcpp.com/mcpp/schedule/#2026 https://…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →