Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leaks - the horror of every programmer?

Tags:

I'm programming a game engine in C++, which also has Lua support.

My biggest horror: Memory leaks.

It's not like my game is already infested with them, I'm rather afraid of them popping out of the ground like mushrooms, when the development is in a late phase and the project huge and complex.

I'm afraid of them because they seem extremely hard to spot for me. Especially in sophisticated systems. If my engine is almost finished, the game runs and the memory gets eaten away, what will I do? Where will I start searching?

  • Is my fear of memory leaks justified?
  • How can one find out where a memory leak lies?
  • Aren't there good tools which help in finding the source of memory leaks today?
like image 242
Cats in Your Code Avatar asked Feb 06 '11 15:02

Cats in Your Code


People also ask

Do all programs have memory leaks?

The reality is that memory leaks can strike any application in any language. They're more common in older or “closer to the metal” languages like C or C++, sure. But all it takes is a visit to one poorly-optimized web page to discover that even a language like JavaScript can have problems with memory leaks.

What is a memory leak in programming?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What is the main cause of memory leaks?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

How are memory leaks harmful?

Memory leaks are bad because your program claims resources and keeps them occupied for its entire lifecycle, even though it does not need them anymore. If you have a static leak the size of X when the program starts and it does not grow over time it's unfortunate, but probably not the end of the world.


2 Answers

How can one find out where a memory leak lies?

Valgrind

like image 118
James Armstrong Avatar answered Oct 07 '22 12:10

James Armstrong


Raw pointers are only one potential cause of memory leaks.

Even if you use smart pointers, like shared_ptr, you can get a leak if you have a cycle - the cure for this is to use a weak_ptr somewhere to break the cycle. Using smart pointers is not a cure-all for memory leaks.

You can also forget a virtual destructor in a base class, and get leaks that way.

Even if there are no problems with new-ed objects not being deleted, a long-running process can grow (and appear to leak) because of address space fragmentation.

Tools like valgrind are very, very useful for finding leaks, but they won't always tell you where the fix should be (e.g. in the case of cycles or objects holding onto smart pointers)

like image 26
Chris Card Avatar answered Oct 07 '22 11:10

Chris Card