Disabling assertions in production builds causes bugs.

Turning Off Assertions Removes the Checks and Keeps the Bugs

I still remember the 3:00 AM pager alert from my time in high-frequency trading—a single, catastrophic crash that wiped out a dozen execution threads because someone thought they were being clever. They had left a massive web of heavy-duty checks active, convinced that assertions in production builds were a free safety net. In reality, those checks had mutated the timing of our hot paths so severely that we weren’t just running slow; we were hitting race conditions that only existed because the assertions were there to mask them. It wasn’t a safety net; it was a landmine.

I’m not here to give you the sanitized, textbook version of how to write unit tests or when to toggle a macro. I want to talk about the actual cost of what you leave in your binary. I’ll show you exactly how the compiler views your sanity checks and why your “protective” code might be the very thing destroying your performance or, worse, hiding the bugs you actually need to find. This is about knowing the rules of the machine before you ship.

Table of Contents

The Ndebug Trap When Conditional Compilation Masks Reality

The Ndebug Trap When Conditional Compilation Masks Reality

Most developers treat `NDEBUG` like a magic switch that cleans up the code for shipping. You define it, the `assert()` calls vanish, and suddenly your binary is leaner. But this is where the mental model starts to fracture. By using conditional compilation for assertions, you aren’t just removing checks; you are fundamentally altering the execution paths of your program. The code you tested in your debug environment is no longer the code that is actually running in the wild.

The real danger lies in how this masks reality. When you rely on `NDEBUG` to handle software invariant checking, you create a bifurcated universe. In your test suite, a violated precondition triggers a clean, immediate abort. In production, that same violation becomes a silent, creeping corruption of state that might not manifest as a crash until three hours later in a completely unrelated module. You aren’t just disabling checks for performance; you are deleting the very scaffolding that keeps your logic upright, leaving you to debug a ghost in the machine.

Software Invariant Checking That Vanishes When You Need It Most

Software Invariant Checking That Vanishes When You Need It Most

The problem with relying solely on `assert()` is that it creates a false sense of security. We use software invariant checking to verify that our assumptions—like a pointer being non-null or a buffer being within bounds—hold true during development. But because these checks are tied to the `NDEBUG` macro, they simply evaporate the moment you flip the switch to a release configuration. You end up with a codebase that is mathematically verified in your test suite, yet behaves like a wild animal in the wild.

This discrepancy between debugging vs production builds is where the most expensive bugs live. When you’re disabling assertions for performance, you aren’t just removing a few branches; you are removing the very guardrails that define your program’s valid states. If an invariant is violated in a production environment, the program won’t stop at the assertion; it will proceed into undefined behavior, corrupting memory or leaking state long before the crash actually occurs. You aren’t just losing speed; you’re losing the ability to know why things went wrong.

Survival Rules for the Real World

  • Stop using `assert()` for anything that isn’t a developer-only sanity check. If the logic depends on that check to prevent a crash or data corruption in the wild, you don’t need an assertion; you need an exception or an error code.
  • Audit your `NDEBUG` usage. If you have complex logic wrapped in `#ifndef NDEBUG`, you are essentially maintaining two different programs. One is what you test, and the other is what your customers actually run. They are rarely the same.
  • Prefer `[[assume]]` (if you’re on C++23) or compiler-specific intrinsics for performance hints. Unlike assertions, these tell the optimizer “this is true, so go nuts,” without the risk of your safety checks vanishing and leaving you blind during a post-mortem.
  • If you absolutely must have checks in production, use a dedicated `LITE_ASSERT` macro that logs to a ring buffer instead of calling `std::abort()`. Dying silently is bad; dying loudly is better; but dying with a breadcrumb trail is the only way to actually fix the bug.
  • Watch out for side effects in your assertion expressions. I’ve seen enough production crashes caused by the fact that a developer put `assert(ptr = allocate_resource())` inside a macro. When `NDEBUG` is defined, that allocation never happens, and the next line hits a null pointer.

The Cost of Ignorance

Stop treating `assert()` as a safety net for production; it is a development-time diagnostic tool that evaporates the moment you flip the `NDEBUG` switch.

If a condition is truly critical to the program’s integrity, don’t hide it behind a macro—use an explicit error-handling path that survives the optimizer.

Beware of “Heisenbugs” that only appear in release builds because your logic relies on checks that the compiler is perfectly happy to prune away.

The Cost of Silence

We have to stop pretending that `assert()` is a complete safety strategy. Between the `NDEBUG` macro stripping your logic away and the compiler optimizing based on the assumption that those invariants actually hold, you aren’t just losing performance—you are losing visibility. When you rely on conditional compilation to handle your error states, you create a bifurcated reality where the code you test is fundamentally different from the code you ship. If an invariant fails in the wild and your assertion was optimized into non-existence, you aren’t just debugging a crash; you are chasing a ghost in the machine that only appears when the lights are turned off.

My advice is to stop treating error detection as a binary choice between “debug mode” and “production speed.” If a condition is truly critical to the integrity of your system, it deserves a mechanism that survives the optimizer. Use structured logging, telemetry, or dedicated runtime checks that don’t vanish under the pressure of a `-O3` flag. Build systems that respect the actual behavior of the machine rather than the idealized version described in your IDE. C++ is a high-performance tool, but it only stays useful if you remain the one in control of the execution flow.

About Ruaridh Kensington-Oyelaran

C++ rewards people who know what the compiler is allowed to do. I write about the rules that bite, the ones nobody mentions until you have already shipped the bug.

More From Author

Deque memory layout explained: growing both ends.

Deque Grows at Both Ends Without Moving Anything