"++*this" crashing

S

srinivas

hi,
i am incrementing a list iterator "m_item" in my .cpp file

m_item._ptr->_next is showing the valid item.

but in the library overloaded function for ++

1 >_Myt_iter operator++(int)
2 > { // postincrement
3 > _Myt_iter _Tmp = *this;
4 > ++*this;
5 > return (_Tmp);
6 > }

(line 4)++*this is crashing...it is not showing any item in its next
pointer.

what is the problem?

thanks,
srinivas
 
O

Ondra Holub

srinivas napsal:
hi,
i am incrementing a list iterator "m_item" in my .cpp file


m_item._ptr->_next is showing the valid item.

but in the library overloaded function for ++

1 >_Myt_iter operator++(int)
2 > { // postincrement
3 > _Myt_iter _Tmp = *this;
4 > ++*this;
5 > return (_Tmp);
6 > }

(line 4)++*this is crashing...it is not showing any item in its next
pointer.

what is the problem?

thanks,
srinivas

You should post here minimized compilable example which crashes.
Without it we can only guess.
 
S

srinivas

hi Ondra
thanks for reply.

is assignment of an iterator to another iterator valid?
i have found this assignment is creating problem.
m_iter_item = iter_item;
m_iter_item++;

srinivas
 
O

Ondra Holub

srinivas napsal:
hi Ondra
thanks for reply.

is assignment of an iterator to another iterator valid?
i have found this assignment is creating problem.

In general, it is valid. I think you have incorrect operator=. I do not
know internal structures of your iterator, so I am only guessing.
 
S

srinivas

but the assignment operator= is not overloaded for list iterators.
so i am thinking assignment has gone wrong.
 
O

Ondra Holub

srinivas napsal:
but the assignment operator= is not overloaded for list iterators.
so i am thinking assignment has gone wrong.

Maybe you have to define your own implementation of assignment
operator. If you are allocating some memory with new and freeing it in
destructor, you definitely need define your own operator= and perform
deep copy.

I cannot say more, because I do not know anything about your
implementation :-(
 
D

Daniel T.

srinivas said:
i am incrementing a list iterator "m_item" in my .cpp file


m_item._ptr->_next is showing the valid item.

but in the library overloaded function for ++

1 >_Myt_iter operator++(int)
2 > { // postincrement
3 > _Myt_iter _Tmp = *this;
4 > ++*this;
5 > return (_Tmp);
6 > }

(line 4)++*this is crashing...it is not showing any item in its next
pointer.

what is the problem?

Line 4 should be: ++(*this);
 
D

Daniel T.

"srinivas said:
hi,
i am incrementing a list iterator "m_item" in my .cpp file


m_item._ptr->_next is showing the valid item.

but in the library overloaded function for ++

1 >_Myt_iter operator++(int)
2 > { // postincrement
3 > _Myt_iter _Tmp = *this;
4 > ++*this;
5 > return (_Tmp);
6 > }

(line 4)++*this is crashing...it is not showing any item in its next
pointer.

what is the problem?

BTW, variables starting with underscore and a capital letter are
reserved. Don't use them unless you are writing a compiler.
 
D

Daniel T.

srinivas said:
is assignment of an iterator to another iterator valid?
i have found this assignment is creating problem.

m_iter_item = iter_item;
m_iter_item++;

Assignment from one object to another of the same type is valid if the
class was written such that it is valid. The above code does not compile
on its own (which does create a problem. :)

You probably need to provide a valid op=.
 
S

srinivas

i have gone through both the list iterator
m_iter_item & Iter_item from begin to end.
using _ptr->_Next and _ptr->_Prev
addresses in both the iterators are identical.

i have no clue why ++*this is crashing.

code in the above case is:
-----------------------------------------
1> class dummy
2> {
3> ..
4> ..
5> private:
6> list<mItem>::iterator m_iter_item;
7> }
8>
9> void dummy::func1()
10> {
11> list<mItem>::iterator iter_item;
12> getItem(&iter_item);
13> m_iter_item = iter_item;
14> ..
15> m_iter_item++;
16> ...
17> }
-----------------------------------------
when i changed the function func1() as

9> void dummy::func1()
10> {
11> list<mItem>::iterator iter_item;
12> getItem(&m_iter_item);
13> ..
14> m_iter_item++;
15> ...
16> }

there is no problem at all.

regards,
srinivas
 
I

Ismo Salonen

srinivas said:
i have gone through both the list iterator
m_iter_item & Iter_item from begin to end.
using _ptr->_Next and _ptr->_Prev
addresses in both the iterators are identical.

i have no clue why ++*this is crashing.

--snip--

Perhaps you are removing items from the list (or adding to it ) ?
Is your iterator still valid ( the value in it pointed in list was
changed or even removed ? )

Or the iterator was already pointing to wrong location ( at or after
list.end() )

I suppose the iterator is normal forward iterator, not reverse_iterator.

Or you have memory corruption, you are overwriting the iterator fields
somehow.

ismo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top