C++ Bug Hunt Medium +20 XP
Raw Pointer Memory Leak
This code leaks memory.
C++Find the bug
1
void createObj() {2
MyClass* obj = new MyClass();3
obj->doWork();4
} // obj destroyed without delete The bug is on line 4
Select the correct fix
Show answer and explanation
Answer: Both A and B fix it
new without delete leaks memory. Use unique_ptr for automatic RAII cleanup, or manually call delete.
Hint: Every new must have a corresponding delete, or use RAII.
What this challenge tests
Bug Hunt in C++: memory-leak, smart-pointers.