Solving random crashes

前端 未结 17 1004
青春惊慌失措
青春惊慌失措 2021-01-31 08:15

I am getting random crashes on my C++ application, it may not crash for a month, and then crash 10 times in a hour, and sometimes it may crash on launch, while sometimes it may

17条回答
  •  悲&欢浪女
    2021-01-31 09:18

    First, you are lucky that your process crashes multiple times in a short time-period. That should make it easy to proceed.

    This is how you proceed.

    • Get a crash dump
    • Isolate a set of potential suspicious functions
    • Tighten up state checking
    • Repeat

    Get a crash dump

    First, you really need to get a crash dump.

    If you don't get crash dumps when it crashes, start with writing a test that produces reliable crash dumps.

    Re-compile the binary with debug symbols or make sure that you can analyze the crash dump with debug symbols.

    Find suspicious functions

    Given that you have a crash dump, look at it in gdb or your favorite debugger and remember to show all threads! It might not be the thread you see in gdb that is buggy.

    Looking at where gdb says your binary crashed, isolate some set of functions you think might cause the problem.

    Looking at multiple crashes and isolating code sections that are commonly active in all of the crashes is a real time-saver.

    Tighten up state checking

    A crash usually happens because some inconsistent state. The best way to proceed is often to tighten the state requirements. You do this the following way.

    For each function you think might cause the problem, document what legal state the input or the object must have on entry to the function. (Do the same for what legal state it must have on exit from the function, but that's not too important).

    If the function contains a loop, document the legal state it needs to have at the beginning of each loop iteration.

    Add asserts for all such expressions of legal state.

    Repeat

    Then repeat the process. If it still crashes outside of your asserts, tighten the asserts further. At some point the process will crash on an assert and not because of some random crash. At this point you can concentrate on trying to figure out what made your program go from a legal state on entry to the function, to an illegal state at the point where the assert happened.

    If you pair the asserts with verbose logging it should be easier to follow what the program does.

提交回复
热议问题