Of Memory Management, Heap Corruption, and C++

后端 未结 12 1886
半阙折子戏
半阙折子戏 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:31

    These are relatively cheap mechanisms for possibly solving the problem:

    1. Keep an eye on my heap corruption question - I'm updating with the answers as they shake out. The first was balancing new[] and delete[], but you're already doing that.
    2. Give valgrind more of a go; it's an excellent tool, and I only wish it was available under Windows. I only slows your program down by about half, which is pretty good compared to the Windows equivalents.
    3. Think about using the Google Performance Tools as a replacement malloc/new.
    4. Have you cleaned out all your object files and started over? Perhaps your make file is... "suboptimal"
    5. You're not assert()ing enough in your code. How do I know that without having seen it? Like flossing, no-one assert()s enough in their code. Add in a validation function for your objects and call that on method start and method end.
    6. Are you compiling -wall? If not, do so.
    7. Find yourself a lint tool like PC-Lint. A small app like yours might fit in the PC-lint demo page, meaning no purchase for you!
    8. Check you're NULLing out pointers after deleteing them. Nobody likes a dangling pointer. Same gig with declared but unallocated pointers.
    9. Stop using arrays. Use a vector instead.
    10. Don't use raw pointers. Use a smart pointer. Don't use auto_ptr! That thing is... surprising; its semantics are very odd. Instead, choose one of the Boost smart pointers, or something out of the Loki library.

提交回复
热议问题