gpt4 book ai didi

c++ - 内存冲突 : SIGSEGV and 'can' t find linker symbol for virtual table. ..'

转载 作者:行者123 更新时间:2023-11-28 05:59:35 28 4
gpt4 key购买 nike

我在 C++ 代码中遇到内存冲突错误,这让我抓狂。我必须使用一些现有的类,它们几乎在其他任何地方都可以正常工作。

我正在尝试制作自定义数组对象的拷贝,而不是稍后修改内部值。但是那个复制操作有问题......

症状如下:

  • Segmentation fault复制之后,但不是立即

  • 警告:can't find linker symbol for virtual table for 'MyClass<T>' value

MyClass<T>与问题部分无关,搜索后发现vtable被覆盖时会出现此错误(link) .

SIGSEGV 出现在这段代码的末尾:

// 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::vector相反 Array<Box> nextBoxes但问题依然存在,只是出现在不同的位置。

这是“遗留”类中的一个:

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(){...}
...

来自崩溃位置的微小调试器信息:

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

(在 Array<T> 类的深处,在哪里并不重要,因为总是像这里一样发生在 new 行,而且总是没有明显的原因)

最佳答案

好吧,有件事逃过了我的注意……过度索引数组主要发生在索引超出范围时。有时数组太短,或者索引太长,或者您创建了错误大小的数组。最后一个案例就发生在这里。

我之所以回答这个问题是因为:

我已经看到引用的警告消息,但是在完全不同的情况下(真正的链接问题,当一个节点实际上从 vtable 中丢失时)。在我的例子中,由于一些错误的数组处理,虚拟表被覆盖了,令人惊讶。

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

我学到了什么:

  • 始终仔细检查容器对象(不仅仅是索引变量)

  • 三重检查当您提出新问题时(是的,该问题在关键行中包含拼写错误)


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

ic 的值是“坏人”,需要深入我的项目才能发现它,但我会解释它:

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

followingItems是需要插入的项目 ids( Arra< int > ) 的列表。 x可以在 [1, allItemCount] 范围内

Box类,Array<bool> items是一个 bool 掩码,它标记一个项目是否在盒子里。显然,items.length() = allItemCount所以:

followingItems.length <= allItemCount

首先,我意识到 ic一直运行到 87而不是 400 .我想复制全部内容,但只测试并添加了前 87 项。考虑到这一点,我发现了主要错误:

Box box(ic, capacity);

第一个参数应该是所有项目的数量,但又是 87 而不是 400。好吧,这很痛苦......

关于c++ - 内存冲突 : SIGSEGV and 'can' t find linker symbol for virtual table. ..',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33544120/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com