简体   繁体   中英

std vector const_iterator assignment Access violation

I'm working on code in which 1 of more variable length object are stored as a vector of all units ie assuming each letter is a unit, and the same letter are the same high level object. the vector may contain something like the following: aaaabbcccdeeffff...

This vector is an internal private variable of a class which has a operator[] defined such that for the vector above

 - obj[0] returns an const_iterator to the first "a"
 - obj[1] to the first "b"
 - obj[2] to the first "c" etc.

const AI::GeneArray::const_iterator& AI::Chromosome::operator[](int index) const {
    GeneArray::const_iterator pGene;
    int cIndex;
    for (cIndex=0,pGene = this->vGenes.cbegin();pGene != this->vGenes.cend();pGene++, cIndex++) {
        if (index == cIndex) { break; }
        (*pGene)->EndOfBlock(pGene); }
    return pGene; }

Then in another function I have the following

AI::GeneArray::const_iterator function = vChromosome[0];

This causes an Access violation Error.

The Call stack's above my function are as follows

AI.exe!std::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > >::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > >(const std::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > > & __that)  + 0x2f byte
AI.exe!std::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12>::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12>(const std::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12> & __that)  + 0x2f bytes
AI.exe!std::_Iterator_base12::_Iterator_base12(const std::_Iterator_base12 & _Right)  Line 118
AI.exe!std::_Iterator_base12::operator=(const std::_Iterator_base12 & _Right)  Line 123 + 0x5 bytes

The final Call is to

_Iterator_base12& operator=(const _Iterator_base12& _Right)
    {   // assign an iterator
        if (_Myproxy != _Right._Myproxy)
            _Adopt(_Right._Myproxy->_Mycont);<- This line
        return (*this);
    }

According to my debugger (Visual C++ 2010 Express)

_Right._Myproxy->
   _Mycont = CXX0030: Error: expression cannot be evaluated
   _Myfirstiter = CXX0030: Error: expression cannot be evaluated

Else where in my project I have similar code using std::list, rather than vector that works correctly

parents = new ChromosomeList::const_iterator[C];
*(parents) = --(this->vChromosomes.cend());
for (int i=1;i<C;i++) {
    *(parents+i) = ChromosomeList::const_iterator((*(parents+i-1)));
    (*(parents+i))--; }

I've checked the value returned by

vChromosome[0]

this value is of the correct type,

const AI::GeneArray::const_iterator &

I've search Google for similar problems, All I've been able to find is problem related to looping through a vector using iterator's

ie

for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)

Such Code in my project is working correctly.

AI::GeneArray::const_iterator function = vChromosome[0];

Should not compile. function is an iterator, but you're attempting to set it to the value of the contents of a value. Are you sure you did not want vChromosome.begin() instead?

Assuming that's just a typo here, your bug would not be at the point where the assignment happens, your bug would be somewhere before that. vChromosome might be empty, for example, in which case attempting to access operator[](0) would lead to undefined behavior. (See if the vector is even valid first!)

(Side note,

parents = new ChromosomeList::const_iterator[C];

why are you managing an array like this manually? Isn't that what vector is for? :) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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