简体   繁体   English

疯狂的C ++向量迭代器

[英]Crazy C++ Vector iterator

I declare: 我声明:

typedef std::tr1::shared_ptr<ClassA> SharedPtr;

And then: 接着:

std::vector<SharedPtr> mList;

And: 和:

typedef std::vector<SharedPtr>::iterator ListIterator;

The return of mList.size() is 0, but when I use iterators, it iterates over the vector which is empty ! mList.size()的返回值为0,但是当我使用迭代器时,它将遍历为空的向量! This is how I use the iterator: 这就是我使用迭代器的方式:

for(ListIterator it = mList.begin(); it!=mList.end(); it++)
    (*it)->someMethod();

It executes the " someMethod() " and then it throws Segmentation Fault. 它执行“ someMethod() ”,然后引发Segmentation Fault。 How iterators is iterating in an empty vector ???? 迭代器如何在空向量中迭代?

More information 更多信息

I'm using GTK, so this is how I pass the main object: 我正在使用GTK,因此这是我传递主要对象的方式:

g_signal_connect(G_OBJECT(widget), "event", G_CALLBACK(&ClassB::fun), this)

The this is the ClassB itself . this 就是ClassB本身

And then I receive it like this: 然后我收到这样的消息:

gboolean ClassB::fun(GtkWidget *widget, GdkEvent *event, ClassB *data)
{
    // The mList is here, and is accessed like this:
    // data->mList
}

The mList is declared as I cited, when I access other attribute, let's say data->xxx it works and it's fine, the problem is occuring only with mList and this attribute is not dynamically allocated. 按照我引用的方式声明了mList ,当我访问其他属性时,假设data->xxx可以正常工作,而且很好,仅在mList出现了问题,并且该属性未动态分配。

I've checked the memory address of the *data and of the this , they're the same address. 我已经检查了*datathis的内存地址,它们是相同的地址。

I've solved the problem, the object class B was being destroyed after some scope. 我已经解决了这个问题,对象类B在某个作用域之后被销毁了。 Anyway, thank you guys ! 无论如何,谢谢大家!

Add this assert before your for loop. 在您的for循环之前添加此断言。 If you trigger it, mList is corrupted. 如果触发它, mList已损坏。 Eg, perhaps the containing class is also corrupted/dead/not what you think it is. 例如,也许包含的类也已损坏/不可用/不是您认为的那样。

assert( mList.size() != 0 || mList.begin() == mList.end() )

Did you paste your for loop exactly ? 你粘贴for循环到底什么 If you accidentally had a stray ; 如果你偶然迷路了; at the end of your for loop, it would in fact seem to execute one iteration and make the call as you're seeing. 实际上,在for循环的最后,它似乎执行了一次迭代并按照您所看到的进行了调用。

Have you printed out the list size directly before the loop to make sure that it is in fact empty? 您是否在循环之前直接打印了列表大小以确保它实际上是空的? the only other option I can think of is that the list isn't in fact empty, but contains one or more garbage elements. 我能想到的唯一其他选择是列表实际上不是空的,而是包含一个或多个垃圾元素。

Maybe not the answer in your case, but beware of spurious trailing semicolons on for loops. 在您的情况下,可能不是答案,但是请注意for循环上的虚假尾部分号。

I often write this and have to give myself a good kicking when I find it... 我经常写这篇文章,当我发现它的时候必须给自己一个好的踢...

for(ListIterator it = mList.begin(); it!=mList.end(); it++);
    (*it)->someMethod();

It could account for the symptom of someMethod being called even when mList is empty (assuming 'it' is in scope from elsewhere somehow). 即使mList为空(假设“ it”在某种程度上在其他范围内),它也可以解释出调用someMethod的症状。

Failing that, I'd guess mList is corrupted before your for loop runs. 失败的话,我猜mList在for循环运行之前已损坏。 IIRC vectors may store begin, end AND size separately, so if something else stomps on "end" (eg zeroing it), then begin !=end, but size == 0. IIRC向量可以分别存储“开始”,“结束”和“大小”,因此,如果在““结束””上有其他问题(例如将其清零),则开始!= end,但是大小== 0。

You could always just rewrite the code in an iterator-free way (OK, I know this could just be masking the issue, but, hey...) 您总是可以以无迭代器的方式重写代码(好的,我知道这可能掩盖了问题,但是,嘿...)

for (int i=0; i< mList.size(); i++)
  mlist[i]->someMethod();

列表是否在循环过程中被修改(可能通过回调)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM