Heap corruption: What could the cause be?

后端 未结 13 1498
时光取名叫无心
时光取名叫无心 2020-12-25 12:56

I am investigating a crash due to heap corruption. As this issue is non-trivial and involves analyzing the stack and dump results, I have decided to do a code review of file

13条回答
  •  [愿得一人]
    2020-12-25 13:32

    Its a common mistake to free() or delete allocated memory more than one. It may help to insert something like *var = NULL after such calls, and to check for != NULL when calling free. Although in C++ its legal to call delete with a NULL variable, calling C - free() will fail.

    Also a common problem is to confuse delete and delete [].

    Variables allocated with new must be released with delete.

    Array allocated with new [] must be released with delete[].

    Also make sure not to mix C- style memory management (malloc, calloc, free) with C++ style memory management (new/delete). In legacy code often both are mixed, but things allocated with the one can not be freed with the other.

    All of these errors will usually not be recognized by a compiler.

提交回复
热议问题