Of Memory Management, Heap Corruption, and C++

后端 未结 12 1888
半阙折子戏
半阙折子戏 2020-12-01 02:54

So, I need some help. I am working on a project in C++. However, I think I have somehow managed to corrupt my heap. This is based on the fact that I added an std::stri

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 03:54

    Oh, if you want to know how to debug the problem, that's simple. First, get a dead chicken. Then, start shaking it.

    Seriously, I haven't found a consistent way to track these kinds of bugs down. Because there's so many potential problems, there's not a simple checklist to go through. However, I would recommend the following:

    1. Get comfortable in a debugger.
    2. Start tromping around in the debugger to see if you can find anything that looks fishy. Check especially to see what's happening during the exampleString = hello; line.
    3. Check to make sure it's actually crashing on the exampleString = hello; line, and not when exiting some enclosing block (which could cause destructors to fire).
    4. Check any pointer magic you might be doing. Pointer arithmetic, casting, etc.
    5. Check all of your allocations and deallocations to make sure they are matched (no double-deallocations).
    6. Make sure you aren't returning any references or pointers to objects on the stack.

    There are lots of other things to try, too. I'm sure some other people will chime in with ideas as well.

提交回复
热议问题