Memory Leak & malloc chunks
How it all started
While debugging a memory leak bug during my summer internship, I found out that one of our APIs returned a raw pointer and thus transferred its ownership to the caller (me). In other words, I'm now responsible of delete
-ing the pointer after my code finishes. While this is a terrible engineering practice, I grew interested in how memory leaks happen, and how delete[]
solves the problem.
After some research & experiments, I wrote this blog post, which hopefully addresses three sets of questions
- What are memory leaks?
- How are objects allocated on the heap? How does
delete[]
know which area of memory to be freed? - How can we prevent memory leaks from happening?