Last week, I started writing about runtime bugs that I’ve encountered in my own code, for my own interest and potential benefit. This post contains a summary of the issues I noticed, and a brief self-review of the week.
Summary of the week
- Changed virtual function prototype results in correct function not being called
- memcpy() into an uninitialized pointer variable
- Bad loop counting
- Failing to store updated state
- Uninitialized class member
- Sliced inheritance
- Undesired printf() output
- Failed to initialize instance-specific member variable correctly because I was calling the wrong constructor
- Use of uninitialized system
- Yet another uninitialized member variable crash
- Unexpected use of class
- Hard-coded array size, no checking
- General disaster
- Initialized, but not in the right order (fallout from Unexpected use of class)
- Local static state is local, static
- Redux: Changed virtual function prototype results in correct function not being called
Looking back over the week, failure to initialize, failure to init at the right time or in the right order stands out as a primary source of bugs. Maybe I need to be asking myself questions like “where did this variable’s state come from” to try and prompt more consideration of this.
Broadly, there is also what appears to be insufficient familiarity with various of C++’s OO features, resulting in silly mistakes. I don’t generally write much OO-style code — this week has been unusual in that way. It is a problem if I can’t use them reliably.
I’m pretty happy with the solution to the “Changed version prototype” situation. Finding ways to prevent possible runtime bugs at compile-time always feels like a worthy achievement.
Thus far, I’ve found the process of logging these bugs to be worthwhile — attempting to explain the bugs helps me to understand them more clearly. I have found myself thinking about the bugs: the solutions I’ve attempted, and how to try to avoid writing the same bugs in the future.
My plan for now is to continue to write these posts. Hopefully, it leads to fewer bugs.