内存管理、堆损坏和 C++

2021-12-21 00:00:00 memory stack c++ heap-memory

所以,我需要一些帮助.我正在用 C++ 开发一个项目.但是,我认为我以某种方式设法破坏了我的堆.这是基于这样一个事实,即我向一个类添加了一个 std::string 并为它分配了另一个 std::string 的值:

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::string to a class and assigning it a value from another std::string:

std::string hello = "Hello, world.
";
/* exampleString = "Hello, world.
" would work fine. */
exampleString = hello;

因堆栈转储而在我的系统上崩溃.所以基本上我需要停止并检查我所有的代码和内存管理内容,然后找出我搞砸的地方.代码库仍然很小(大约 1000 行),所以这很容易做到.

crashes on my system with a stack dump. So basically I need to stop and go through all my code and memory management stuff and find out where I've screwed up. The codebase is still small (about 1000 lines), so this is easily do-able.

不过,我对这种东西很着迷,所以我想我会把它扔在那里.我在 Linux 系统上使用了 valgrind,虽然不完全知道我在做什么,但它确实报告了 std::string 的析构函数是一个无效的自由.我不得不承认从 Google 搜索中得到了堆损坏"这个词;任何关于此类内容的通用文章也将不胜感激.

Still, I'm over my head with this kind of stuff, so I thought I'd throw it out there. I'm on a Linux system and have poked around with valgrind, and while not knowing completely what I'm doing, it did report that the std::string's destructor was an invalid free. I have to admit to getting the term 'Heap Corruption' from a Google search; any general purpose articles on this sort of stuff would be appreciated as well.

(在rm -rf ProjectDir之前,在C#中再做一次:D)

(In before rm -rf ProjectDir, do again in C# :D)

我没有说清楚,但我要求的是诊断这类记忆问题的建议.我知道 std::string 的东西是对的,所以这是我做过的事情(或者是一个错误,但 Select 没有问题).我敢肯定我可以检查我写的代码,你们非常聪明的人很快就会看到问题,但我想将这种代码分析添加到我的工具箱"中,就像这样.

I haven't made it clear, but what I'm asking for are ways an advice of diagnosing these sort of memory problems. I know the std::string stuff is right, so it's something I've done (or a bug, but there's Not A Problem With Select). I'm sure I could check the code I've written up and you very smart folks would see the problem in no time, but I want to add this kind of code analysis to my 'toolbox', as it were.

推荐答案

这些是可能解决问题的相对廉价的机制:

These are relatively cheap mechanisms for possibly solving the problem:

  1. 关注我的堆损坏问题 - 我'当他们摇晃时,我会更新答案.第一个是平衡 new[]delete[],但您已经在这样做了.
  2. 多尝试valgrind;这是一个很好的工具,我只希望它在 Windows 下可用.我只将您的程序减慢了大约一半,与 Windows 等效程序相比,这是相当不错的.
  3. 考虑使用 Google Performance Tools 作为替代 malloc/new.
  4. 您是否清除了所有目标文件并重新开始?也许你的 make 文件是......次优"
  5. 您在代码中assert()不够.没看过怎么知道呢?就像使用牙线一样,没有一个 assert() 在他们的代码中就足够了.为您的对象添加验证函数,并在方法开始和方法结束时调用该函数.
  6. 您是否编译-wall?如果没有,请这样做.
  7. 为自己寻找一个 lint 工具,例如 PC-Lint.像您这样的小应用程序可能适合 PC-lint 演示页面,这意味着您无需购买!
  8. 检查是否在删除指针后将指针置为 NULL.没有人喜欢悬垂的指针.与声明但未分配的指针相同的演出.
  9. 停止使用数组.改用矢量.
  10. 不要使用原始指针.使用智能指针.不要使用auto_ptr!那件事……令人惊讶;它的语义很奇怪.相反,选择Boost智能指针之一,或Loki 库之外的内容.
  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.

相关文章