8

I am currently working on a large project, and I spend most of the time debugging. While debugging is a normal process, there are bugs, that are unstable, and these bugs are the greatest pain for the developer. The program does not work, well, sometimes... Sometimes it does, and there is nothing you can do about it.

What can be done about these bugs? Most common debugging tools (interactive debuggers, watches, log messages) may lead you nowhere, because the bug will disappear ... just to appear once again, later. That is why I am asking for some heuristics: what are the most common reasons for such bugs? What suspicious code should we investigate to locate such a bugs?

Let me start the list:

  1. using uninitialized variables. Common misprints like mMember = mMember;
  2. thread synchronization. Sometimes it can be a matter of luck;
  3. working with non-smart pointers, dereferencing invalid ones;

what else?

0

9 Answers 9

23

IME the underlying problem in many projects is that developers use low-level features of C++ like manual memory management, C-style string handling, etc. even though they are very rarely ever necessary (and then only well encapsulated in classes). This leads to memory corruption, invalid pointers, buffer overflows, resource leaks and whatnot. All the while nice and clean high-level constructs are available.

I was part of the team for a large (several MLoC) application for several years and the number of crashing bugs for different parts of the application nicely correlated to the programming style used within these parts. When asked why they wouldn't change their programming style some of the culprits answered that their style in general yields more performance. (Not only is this wrong, it's also a fact that customers rather have a more stable but slower program than a fast one that keeps crashing on them. Also, most of their code wasn't even required to be fast...)

As for multi-threading: I don't feel expert enough to offer solutions here, but I think Herb Sutter's Effective Concurrency columns are a very worthwhile read on the subject.

Edit to address the discussions in the comments:

I did not write that "C-style string handling is not more performant". (Certainly a lot of negation in this sentence, but since I feel misread, I try to be precise.) What I said is that high level constructs are not in general less performant: std::vector isn't in general slower than manually doing dynamically allocated C arrays, since it is a dynamically allocated C array. Of course, there are cases where something coded according to special requirements will perform better than any general solution -- but that doesn't necessarily mean you'll have to resort to manual memory management. This is why I wrote that, if such things are necessary, then only well-encapsulated in classes.

But what's even more important: in most code the difference doesn't matter. Whether a button depresses 0.01secs after someone clicked it or 0.05secs simply doesn't matter, so even a factor 5 speed gain is irrelevant in the button's code. Whether the code crashes, however, always matters.

To sum up my argument: First make it work correctly. This is best done using well-proven off-the-shelf building blocks. Then measure. Then improve performance where it matters, using well-proven off-the-shelf idioms.

10
  • I must concur that the problems I spend most time with are concurrency problems in other people's overly unstructured software.
    – xtofl
    Commented Aug 28, 2009 at 13:06
  • Small objection to your claim that C string style is not more performant...I got a 10x speedup changing code that stored a vector of strings to code that stored a vector of const char * begin,end pairs.
    – Zan Lynx
    Commented Aug 29, 2009 at 0:27
  • 2
    @Zan Lynx: Yes, and you can get a huge speedup by using string instead of char* in some cases too. Like, say, when you need the size of the string. Constant time .size() beats linear time strlen(). Of course you can get a 10x speedup by fixing inefficient code. It is not char* that was magically efficient, it was the vector of strings that was a stupid way to structure the data. Commented Aug 29, 2009 at 0:33
  • 1
    @nimrodm Why would you want to debug the standard library? Surely it's main advantage is that has already been debugged?
    – anon
    Commented Aug 30, 2009 at 20:35
  • 1
    +1: Excellent answer. One general point to some of these comments: It's usually easier to take a simple working piece of slow code and optimise it than it is to take a fast piece of broken code and make it work. Commented Sep 1, 2009 at 8:20
6

Race conditions.

These are one of the few things that still sends a shiver down my spine when it comes up in debugging (or in the issue tracker). Inherently horrible to debug, and extremely easy to create. The three most common causes of bugs in my C++ software have been race conditions, reliance on uninitialised memory, and reliance on static constructor order.

And if you don't know what race conditions are, chances are they're the cause of your instability ;)

5

I was actually going to post a question that asked exactly the opposite - do others find, as I do, that you spend almost no time using the debugger when working with C++? I honestly cannot remember the last time I used one - it must have been about six months ago.

Frankly, if you spend most of the time in the debugger, I think there is something very wrong with your basic coding practices.

13
  • 22
    I cannot agree. Blessed are those programmers, who have to support only their own (perfectly designed) code. Most of the programmers have to support legacy (bad, undocumented) code as well.
    – SadSido
    Commented Aug 28, 2009 at 12:30
  • 1
    I concur - good code can be proven to be correct (or near enough) whereas most C and C++ code that is written is not done that rigorously. This means you end up having to deal with unexpected corner cases that make no sense, and then the need for a debugger comes in.
    – Kylotan
    Commented Aug 28, 2009 at 12:31
  • 2
    Most of the legacy code I've worked on in the past 15 years has been heavily multi-threaded middleware. In that environment, a debugger is worse than useless - it alters the timing of the code and actually hides bugs.
    – anon
    Commented Aug 28, 2009 at 12:38
  • 2
    @Adrian. Never. For most projects I've worked on it would have been impossible. It's one of these "best practices", promulgated by the otherwise sensible Steve Maguire, that I've never believed in.
    – anon
    Commented Aug 28, 2009 at 19:37
  • 1
    @Neil: Of course it's not necessary to use a debugger, but you must have some way of working out the program flow and inspecting variables - even if you just use "std::cout". What do you use to get this information? Of course maybe you're able to digest and execute the entire program in your head - but that really cannot be general advice for the rest of us mere mortals! :) Commented Sep 1, 2009 at 8:32
3

If you are really in a position where you already have bad code that breaks, the best plan is probably to throw as many tools at it as you can (OS/lib-level memory checking, automated testing, logging, core dumps, etc) to find the problem areas. Then rewrite the code to do something more deterministic. Most of the bugs come from people doing things that mostly work most of the time, but C++ offers stronger guarantees if you use the right tools and approaches.

3

Haven't seen this one mentioned yet:

  • Inheriting from a class that does not have a virtual destructor.
1
  • +1. Just grep the "undefined behaviour" from the C++ standard and receive the list of reasons for the floating bugs...
    – SadSido
    Commented Aug 29, 2009 at 10:00
2
  • Reading from uncached memory while a cache line is being written back over the memory (This is a right bastard to find).
  • Buffer overwrites
  • Stack overflows!

The only 3 i can think of at the mo ... may edit later :)

1
  • buffer overflows
  • using pointers to deleted objects
  • returning invalid references or references to out of scope objects
  • unhandled exceptions
  • resource leaks (not only memory)
  • infinite recursion

  • dynamic libraries version mismatch

1
  • 1
    I think both infinite recursion and unhandled exceptions don't count. They are usually reproduced (therefore, tracked) easily.
    – SadSido
    Commented Aug 28, 2009 at 12:32
1

Not really a C++ issue but seen in a C/C++ project.

The trickiest issue I had to deal with was an initialization issue when starting up the OS on our platform that lead to unusual crashes. It took years before we found out what happened. Before that we ran the system overnight and if it didn't crash, then it was normally okay. Luckily, the OS isn't sold anymore.

0

addresses and memory used before allocation or after deallocation, segmentation faults, arrayoutofbounds, offset, threadlocks, unintelligible operator overloading, inline assembly, void exit and void in general where return values are desired complicates where math.h functions are worth a look since all math.h functions both have working arguments and return values compared to other library overly void, emptiness tests, nils, nulls and voids. 4 general conventions I recommend are return values, arguments, ternary choices and invertible changes. Faultprone to avoid are vectors (use arrays instead) void with empty arguments and in my subjective opinion I avoid the switch statement in favor of more intelligible or readable if...elseif or more abstract "is".

C++ also has rather lousy forward compatibility compared to scripts and interpreted, to try a decade old Java it still runs unchanged and safe in later vm.

1
  • Actually, arrays are fault prone, and vectors, not so much. After all, you yourself specify arrayoutofbounds as a big cause of bugs; but vectors, not arrays, warn you with ASSERT errors when you are out of bounds.
    – CodeLurker
    Commented Sep 30, 2018 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.