C++ Runtime and Debugging: Exceptions, Critical Sections, Memory Leaks and Crash Dumps
Company: Autodesk
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Technical Screen
In a technical round for a senior C++ role, the interviewer moves from language features to runtime behavior and debugging. Answer the four questions below for a large C++ application that runs for hours at a time.
### Clarifying Questions
- Which platforms does the application ship on (Windows, Linux, macOS)? Leak tools, debuggers and dump formats differ between them.
- Is the dump from a crash, or was it captured from a running process, for example because its memory kept growing?
- Can the problem be reproduced in an instrumented test build, or is it seen only in release builds in the field?
### Part 1: How C++ exceptions are implemented
How are exceptions implemented in C++? Explain what happens between a `throw` and the matching `catch`: where the exception object is stored, how the runtime finds the handler, how destructors of local objects run on the way, and why the usual implementation is called zero-cost. What happens when an exception leaves a `noexcept` function, or when a destructor throws while another exception is propagating?
```hint What the compiler records
Consider what the compiler must record for each function so that the runtime can walk the stack, find a handler and run destructors without extra code executing on every call.
```
#### What This Part Should Cover
- Where the exception object lives and the runtime calls behind `throw` and `catch`.
- Table-driven stack unwinding: finding the handler, then running cleanups.
- The cost model of the zero-cost approach and the cases that end in `std::terminate`.
### Part 2: Critical sections
What is a critical section? Show how you protect shared data in C++, explain what goes wrong without protection, and compare the options: `std::mutex`, `std::atomic`, a reader-writer lock and the Windows `CRITICAL_SECTION`. How do you keep critical sections from causing deadlocks or heavy contention?
```hint Make the race concrete
Write out the load, add and store behind `counter++` for two threads and find an interleaving that loses an update.
```
#### What This Part Should Cover
- The definition and a concrete race, including why a data race is undefined behavior in C++.
- RAII locking and when each primitive fits.
- Deadlock avoidance and keeping protected regions short.
### Part 3: Detecting memory leaks
How do you detect memory leaks in a C++ program, and which tools do you use? Cover both a leak you can reproduce in a test and slow memory growth that appears only after long runs. How do you tell a true leak apart from memory that is still referenced but no longer needed?
```hint Who still points at it
Separate memory that nothing references any more from memory a container or cache still holds, and ask which tools can see each kind.
```
#### What This Part Should Cover
- Instrumented tools for reproducible leaks and what their reports contain.
- Heap profiling and snapshot comparison for long-running growth.
- Unreachable leaks versus unbounded growth of reachable memory.
- Coding practices and automated checks that prevent leaks.
### Part 4: Working from a dump
You are given a dump from the application. How do you approach it? Describe what you need besides the dump file, the first things you look at, and how you move from the faulting thread to a root cause. How does the approach change if the dump was captured because memory kept growing instead of because the process crashed?
```hint Before opening it
List what must match the dump exactly before any stack trace it shows can be trusted.
```
#### What This Part Should Cover
- Prerequisites: matching binaries, symbols and source.
- A triage order: exception record, faulting stack, registers and locals, other threads.
- Common crash signatures and how to confirm the cause.
- Heap analysis when the dump is about memory growth.
### What a Strong Answer Covers
- Accurate runtime mechanics for exception handling and synchronization.
- Named tools and commands for the platforms in use, with what each one detects.
- A systematic path from symptom to root cause, and from root cause to a test that keeps the bug fixed.
- Prevention through ownership rules, RAII and instrumented test runs.
### Follow-up Questions
- A crash dump shows the faulting thread inside the heap allocator. What does that suggest, and what do you do next?
- How should a destructor report a failure if it must not throw?
- How would you find the cause of a deadlock from a dump of a hung process?
Overview: C++ runtime and debugging questions: how exceptions are implemented, what a critical section is and how to protect one, how to detect memory leaks and which tools to use, and how to approach a crash or memory dump. It tests practical debugging method on large, long-running native applications.
Read the full Autodesk Software Engineer interview experience this question came from