内存管理,堆腐败和C ++ [英] Of Memory Management, Heap Corruption, and C++

查看:248
本文介绍了内存管理,堆腐败和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要一些帮助。我正在一个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 off the fact that I added a std::string to a class and assigning it a value from another std::string:

std::string hello = "Hello, world.\n";
/* exampleString = "Hello, world.\n" 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东西是正确的,所以这是我做过的(或一个错误,但没有一个问题与选择)。我相信我可以吸取我写的代码,你很聪明的人会看到这个问题在任何时间,但我想添加这种类型的代码分析到我的'工具箱',因为它。 >

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 chuck 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下可用。

  3. 想想使用 Google Performance Tools 作为替换malloc /新。

  4. 您是否已清除所有对象文件并重新开始?也许你的make文件是...次优

  5. 你不是 assert()我怎么知道没有看到它?像牙线,没有一个 assert()足够在他们的代码。

  6. compiling -wall

  7. 找到一个lint工具,例如 PC-Lint 。像您这样的小应用程式可能适用于 PC-lint demo 页面,表示没有购买!

  8. 在删除它们之后重新舍弃指针。没有人喜欢悬挂指针。与声明但未分配的指针相同。

  9. 停止使用数组。请改用向量

  10. t使用原始指针。使用智能指针。不要使用 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.

这篇关于内存管理,堆腐败和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆