内存冲突:SIGSEGV和'找不到链接符号为虚拟表......“ [英] Memory violation: SIGSEGV and 'can't find linker symbol for virtual table...'

查看:1029
本文介绍了内存冲突:SIGSEGV和'找不到链接符号为虚拟表......“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和内存冲突错误在我的C ++ code奋斗,这让我疯了。我必须使用一些现有的类,它们做工精细,几乎其他任何地方。

I struggle with a memory violation error in my C++ code, and it makes me crazy. I have to use some existing classes and they work fine almost everywhere else.

我试图做一个自定义的数组对象的副本,比修改后的内在价值。但蹊跷的是复制操作...

I am trying to make a copy of a custom Array object, than modify inner values later. But there is something wrong with that copy operation...

症状如下:


  • 分段故障复制后,却没有立即

警告:找不到虚拟表链接符号'MyClass的< T>'值

MyClass的< T> 无关的问题的一部分,搜索后,我发现,当在虚函数表被覆盖可能会出现此错误 (link).

MyClass<T> has nothing to do with the problematic part, and after searching I found that this error could appear when the vtable was overwritten (link).

该SIGSEGV出现在这个片段的末尾:

The SIGSEGV appears at the end of this snippet:

// New boxes based on previous content, so first make a copy
Array<Box> nextBoxes(size);
int ic = followingItems.length();  // Array<int> followingItems() : some item id
for (int b = 0; b < size; ++b) {
    Box box(ic, capacity);
    const Box& oBox = currentBoxes[b];  // Array<Box> currentBoxes(size);
    for (int i = 0; i < ic; ++i) {
        if (oBox[i])
            box.add(i);
    }
    nextBoxes.add(box);
}

createConfig(nextBoxes, nextItems);
...
generateCostMatrix(nextBoxes, costMatrix); // <--[SIGSEGV] without any reason, variables are fine

而这正是我完全失去了。我试图用的std ::矢量而不是阵列&LT;盒及GT; nextBoxes ,但依然存在,只是出现在不同的位置的问题。

And that's where I am totally lost. I tried to use std::vector instead Array<Box> nextBoxes but the problem remained, just appeared at a different location.

下面是一个来自传统类:

Here is one from the 'legacy' classes:

class Box 
{
    Array<bool> items;  // mask of all tools
    int capacity, itemCount, count;
public:
    Box();
    Box(int num, int cap)
      : items(num), capacity(cap), itemCount(num), count(0)
    {
        for (int i = 0; i < num; i++)
            items.add(false);
    }
    Box(const Box& value){...}
    ~Box(){...}
    ...

从碰撞位置的微小调试信息:

A tiny debugger info from the crash location:

array = new T[maxlen]
// values: array=0x0, maxlen=30, len=0 --> looks OK

(深某处阵列&LT; T&GT; 类,它并没有真正的问题在哪里,因为总是发生在该行喜欢这里,始终没有明显理由)

(somewhere deep in the Array<T> class, it doesn't really matter where, because always happens in the line new like here, and always without visible reason)

推荐答案

好了,东西逃过我的注意力......
过索引阵列发生大多是当你的索引超出范围。有时候阵列太短,或者索引太长,或者创建了错误的大小的数组。而这最后一种情况发生在这里。

Well, something escaped my attention... Over-indexing the array happens mostly when your index is out of range. Sometimes the array is too short, or the index is too long, or you create the wrong sized array. And this last case happened here.

为什么我回答了这个问题的原因是:

我见过的报价警告消息,但在一个完全不同的场景(实联的问题,当一个节点实际上从虚函数表丢失)。在我的情况下,虚拟表被覆盖,和惊喜,因为一些不好的阵列处理。

I've seen the quoted warning message, but in a totally different scenario (real linking issue, when a node was actually missing from vtable). In my case the virtual table was overwritten, and surprise, because of some bad array handling.

就为未来的Google,这可以有效调试SIGSEGV其中
  出现在一个奇怪的位置。

Just for the future googlers, this can useful to debug a SIGSEGV which appears at a weird location.

我所学到的:


  • 总是仔细检查的容器对象(而不仅仅是指数的变量)

  • always double-check the container object (not just the index variable)

三重检查的你方的来源,当你问一个新的问题(是的,这个问题包含在临界线错字)

triple-check your quoted source when you ask a new question (yes, the question contained a typo in a critical line)

这里是解决方案,这是不那么重要,但为了完整性...

And here is the solution, which is not so important, but for completeness...

IC 的值是'坏人',需要深入在我的项目被发现,但我会解释它:

The value of ic was the 'bad guy', one needs to go deep in my project to spot it, but i'll explain it:

int ic = followingItems.length(); // where Arra<int> followingItems(x);

followingItems 是项目的列表 IDS 阿拉&LT; INT &GT; ),这需要插入。
X 可在范围 [1,allItemCount]

The followingItemsis a list of the item ids(Arra<int>) which need to be inserted. x can be in the range [1, allItemCount]

类,阵列&LT;布尔&GT;项目是一个布尔值面具,这标志着项目是否在框中与否。东北角, items.length()= allItemCount 这样:

In the Box class, Array<bool> items is a boolean mask which marks whether an item is in the box or not. Obliviously, items.length() = allItemCount so:

followingItems.length <= allItemCount

首先,我意识到, IC 正在运行,直到 87 而不是 400 。我想的全部内容进行复制,但只只是第87项进行了测试,并补充道。
有了这个在我心中,我发现的主要缺陷:

First, I realized that ic was running until 87 instead of 400. I wanted to copy the full content, but only just the first 87 item was tested and added. With this in my mind, I spot the main bug:

Box box(ic, capacity);

当第一参数应该是所有的项目数量,但是87,而不是400一次。嗯,这是pretty痛苦......

Where the first param should be the number of all items, but was 87 instead of 400 again. Well, that's pretty painful...

这篇关于内存冲突:SIGSEGV和'找不到链接符号为虚拟表......“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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