Skip to main content

One post tagged with "c++"

View All Tags

· 17 min read
Ye Shu

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

  1. What are memory leaks?
  2. How are objects allocated on the heap? How does delete[] know which area of memory to be freed?
  3. How can we prevent memory leaks from happening?