We help IT Professionals succeed at work.

Question on using Vector iterator

ambuli
ambuli asked
on
Hi Experts,

I am using the following function to open numbers of file descriptors stored in a vector.  When I run the program, it crashes on this function.  If I check the size of the vector, it says 3.  But, the loop seems to go more than 3 times before crashing.  Can you help me resolve this.  Thank you.

typedef vector<SomeObject> ObjectsVector_t;
typedef ObjectsVector_t::iterator ObjectsVectorIter_t;

bool Reader::openObjects()
{

    cout << "Size is = " << _objectsFactory->getObjectVector().size() << endl;
    for (PPSObjectsVectorIter_t iter = _objectsFactory->getObjectVector().begin(); iter != _objectsFactory->getObjectVector().end(); iter++)
    {
        (*iter).openFd();
    }
}

Open in new window

Comment
Watch Question

Senior Software Engineer (Avast)
Commented:
>> But, the loop seems to go more than 3 times before crashing
How did you assert that?

>> _objectsFactory->getObjectVector()
I presume that always returns a reference to the same vector and isn't constructing a new one on each call?

Author

Commented:
Thanks Evilirix.  I was doing the initialization in the getObjectVector() incorrectly.  Once I resolved that it works.  Thanks.