C++定义两个多重映射会导致程序崩溃

发布于 2024-09-10 05:00:31 字数 609 浏览 3 评论 0原文

这让我非常困惑。在我的类声明中,我有两行:

std::multimap<int, int> commands;
std::multimap<std::string, std::string> config;

代码编译没有问题,但是当我运行它时,我收到以下错误:

*** glibc detected *** ./antares: free(): invalid pointer: 0xb5ac1b64 ***

看起来很简单,但它与稍后如何处理这两个变量无关。我删除了其余代码中对变量的所有引用 - 仍然崩溃。我注释掉了其中一行——无论是哪一行,程序运行都没有问题。错误怎么可能不是与任何一个特定变量有关呢?我假设 STL 中没有错误,但我已经不知道我的代码如何做到这一点。

这让我很困惑,所以我很感激你能提供的任何帮助。 Wyatt

编辑:我并不是说STL 有问题,那只是我有点油嘴滑舌。我知道错误存在于我的代码中,我想知道的是 - 声明未引用的变量会导致它崩溃,可能会出现什么问题?为什么这会影响我的代码?

我的代码有几千行长,所以不值得任何人花时间阅读它,我只是在寻找有人为我指明正确的方向。

This is utterly mystifying me. I have, in my class declaration, two lines:

std::multimap<int, int> commands;
std::multimap<std::string, std::string> config;

The code compiles without issue, but when I run it, I get the following error:

*** glibc detected *** ./antares: free(): invalid pointer: 0xb5ac1b64 ***

Seems simple enough, except that it has nothing to do with how the two variables are later handled. I removed all references in the rest of the code to the variables - still crashed. I commented out one of the lines - either one, and the program ran without issue. How can the error not be with either particular variable? I'm working under the assumption that there isn't a bug in STL, but I've run out of ideas on how my code could possibly be doing this.

This one has me stumped, so I'd appreciate any help you can provide.
Wyatt

EDIT: I'm not suggesting there's a problem with STL, that was just me being a bit glib. I know the bug is in my code, what I want to know is - what could possibly be wrong that declaring an unreferenced variable would cause it to crash? Why would that affect my code at all?

My code is a few thousand lines long, so it's not really worth anyone's time reading through it, I'm just looking for someone to point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2024-09-17 05:00:31

您认为问题不在 GCC 或 STL 中是正确的。但是,如果映射导致 free 错误,则您的其他代码可能是堆栈粉碎(或堆粉碎)。一个真正值得追捕的可怕错误。堆栈粉碎最糟糕的部分是破坏的对象不是有错误的对象。

以下是一些调试技巧。

  • valgrind 下运行应用程序。
  • 定义 _GLIBCXX_DEBUG 以启用 stl 调试,
  • 添加 MALLOC_CHECK_=1 作为环境变量。这将为您提供更好的 malloc 错误消息。更多信息此处
  • 在极少数情况下,我能够添加内存监视到即将被粉碎的位置。但很少有人能够预测粉碎会发生在哪里。

You're correct to assume the problem isn't in GCC or the STL. However, if the maps are causing free errors, your other code is likely stack smashing (or heap smashing). A truly terrible bug to chase down. The worse part about stack smashing is the object that breaks is not the object with the bug.

Here are some debugging tips.

  • Run the app under valgrind.
  • define _GLIBCXX_DEBUG to enable stl debugging
  • add MALLOC_CHECK_=1 as an environment variable. This will give you better malloc error messages. More info here.
  • On rare occasions I have been able to add a memory watch to the location that will be smashed. But it is rare when you can predict where the smashing will occur.
甜嗑 2024-09-17 05:00:31

你是对的:崩溃不是来自这两行——它们只是让它可见。

以下是诊断此问题的方法:

  • 首先,保留变量的定义(使程序崩溃)

  • 第二,删除或禁用其他部分代码,直到崩溃停止发生。然后您就会知道损坏内存的大致区域。

  • 第三(一旦你有一个区域,当禁用时停止崩溃)开始启用它的一部分,直到崩溃再次发生。

编辑:我想说你的问题是包含两个多重映射的代码(缺少复制构造函数或赋值运算符或类似的东西)。这只是一个疯狂的猜测,所以不要对此抱有太多的怀疑。

You are right: the crash is not from these two lines - they just make it visible.

Here's how to diagnose this problem:

  • first, leave your variables defined (make your program crash)

  • second, remove or disable other parts of your code until the crash stops happening. Then you will know an approximate area that corrupts your memory.

  • third (once you have an area that when disabled stops the crash) start enabling parts of it until the crash happens again.

Edit: I'd say your problem is with code that contains your two multimaps (a copy constructor or assignment operator is missing or something like that). It's just a wild guess so don't put much stock on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文