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

Of Memory Management, Heap Corruption, and C++(内存管理、堆损坏和 C++)
本文介绍了内存管理、堆损坏和 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

所以,我需要一些帮助.我正在用 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)