Memory violation: SIGSEGV and 'can't find linker symbol for virtual table…'

随声附和 提交于 2019-12-12 02:49:44

问题


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...

The symptoms are the following:

  • Segmentation fault after the copy, but not immediately

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

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).

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

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

(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)


回答1:


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.

The reason why I answered this question is that:

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.

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

What I learned:

  • 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...

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);

The followingItemsis a list of the item ids(Arra<int>) which need to be inserted. x can be in the range [1, 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

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);

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



来源:https://stackoverflow.com/questions/33544120/memory-violation-sigsegv-and-cant-find-linker-symbol-for-virtual-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!