Handling pointers with references
While doing some refactoring on soothsayer over the weekend, I was once again reassured that one of the software engineering principles that I always adopt when designing systems still holds true.
The principle in question is the age old tips of preferring C++ references to pointers. As the excellent C++ FAQ Lite simply puts it: ``Use references when you can, and pointers when you have to''.
I had to switch from references to pointers in order to add a new feature. This required a refactoring of the relationships among some core components in the system.
Having made significant changes to the lifetime of many objects, I thought it wise to run the demo program through a memory leak detector. Valgrind is such a tool, and a great tool at that.
Sure enough, I did forget to clean up after a couple of objects... The leaks where diligently reported by valgrind's memcheck tool and promptly fixed by me.
Valgrind also found a set of leaks predating the refactor work. The lesson here is to always use a memory checker. I am going to add an automated test to run the soothsayer simulator through valgrind and have its reported automatically checked for leaks.
- Matteo Vescovi's blog
- Login to post comments