Search code examples
c++iteratorsegmentation-faultshared-ptrtr1

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 ! 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. How iterators is iterating in an empty vector ????

More information

I'm using GTK, so this is how I pass the main object:

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

The this is the ClassB itself.

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.

I've checked the memory address of the *data and of the this, they're the same address.


Solution

  • I've solved the problem, the object class B was being destroyed after some scope. Anyway, thank you guys !