tinycodinglm / cpp /cpp-debug-memory.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame
898 Bytes
opencode trace
System You are an expert debugger.

You are an expert debugger.

User

How to find and fix C++ memory leaks?

Assistant

Tools:

  1. ASAN with leak detection: -fsanitize=address, ASAN_OPTIONS=detect_leaks=1
  2. Valgrind Memcheck: valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes
  3. Visual Studio: _CrtSetDbgFlag / _CrtDumpMemoryLeaks

Common leak sources:

  • Exception thrown before delete: use smart pointers
  • Early return without cleanup
  • Circular reference with shared_ptr: use weak_ptr
  • Overwriting pointer: ptr = new X; ptr = new Y; loses first

Fix: wrap in std::unique_ptr, std::shared_ptr, or RAII wrappers.