Debugging question

Hello,

I am writing a program of scientific simulation on Visual Studio. In my simulation there is a big vector of pointers who point to a big number of objects each one consists of some vectors.

When I run the simulation, sometimes it works normally, sometimes it throw an exception by citing "Vector iterator out of range" or "vector iterator not dereferencable".

The code is very long so I cannot put it here. I just put a fraction of the debug file called "vector" automatically made by Visual Studio. Because I don't know what it means and how I can find my error through this.

Could someone knows this please tell me how to debug this error? Thanks in advance.

1
2
3
4
5
6
7
8
9
10
	_Myiter& operator+=(difference_type _Off)
		{	// increment by integer
 #if _ITERATOR_DEBUG_LEVEL == 2
		if (this->_Getcont() == 0
			|| this->_Ptr + _Off < ((_Myvec *)this->_Getcont())->_Myfirst
			|| ((_Myvec *)this->_Getcont())->_Mylast < this->_Ptr + _Off)
			{	// report error
			_DEBUG_ERROR("vector iterator + offset out of range");
			_SCL_SECURE_OUT_OF_RANGE;
			}
The error is a logic error in your code, not that of std::vector.
Could you please explain a bit more?
The code you're showing us is the part of code of std::vector that displays an error if ever the call to the iterator's operator+=() causes the iterator to be out of range.

You need to find the part of your code that tried to increment an iterator just before the error occured.
Thanks for your explanation.

My simulation is based on some random data generated by a generator of c++. And sometimes it works well, sometimes it breaks because of "vector iterator" problem (1 chance out of 15 it breaks)

Does anybody know in this context how can I detect the source of error, please?
Trace the logic of your code to find out where it's possible that an existing iterator is invalidated.

In my experience, these types of errors are usually found in or around code that adds or removes (mostly removes) elements from vectors.

Use a debugger. It should break when the exception is thrown, and you can travel down the callstack until you get to your code to see exactly where the logic error occurs.
Thanks for your tips, cire.

I am new for debugging technics. The debugger of Visual Studio (that I only know a bit) throw exception when there is an error. Then it shows the definition of the vector class (as I cited in my first post), or some exception code which seems show the definition of the function that manages throw exception... never tells where is the error...

I tried to place some break points in my code is locate the error code. However, each simulation contains a loop of 1000 times calculation, so it's impossible to push F5 all the time in the loop...

Here is the text given by debugger when the exception is thrown:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>

_STD_BEGIN

#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
	{	// report error and die
        if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, L"%s", message)==1)
        {
            ::_CrtDbgBreak();
        }
	}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
	{	// report error and die
        _Debug_message((wchar_t *) message, (wchar_t *) file, line);
	}

#endif

_STD_END

Does anyone have an idea what it is about, please?
Last edited on
Here is the call stack of debugger when the exception is thrown:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>	msvcp110d.dll!std::_Debug_message(const wchar_t * message, const wchar_t * file, unsigned int line) Line 15	                      C++
 	ffm1.exe!std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<CAgent *> > >::operator+=(int _Off) Line 159	                      C++
 	ffm1.exe!std::_Vector_iterator<std::_Vector_val<std::_Simple_types<CAgent *> > >::operator+=(int _Off) Line 361	                        C++
 	ffm1.exe!std::_Vector_iterator<std::_Vector_val<std::_Simple_types<CAgent *> > >::operator-=(int _Off) Line 373	                          C++
 	ffm1.exe!std::_Vector_iterator<std::_Vector_val<std::_Simple_types<CAgent *> > >::operator-(int _Off) Line 378	                                             C++
 	ffm1.exe!Fshare(std::vector<CAgent *,std::allocator<CAgent *> > & v, const int nbe, const int nbi, const double lambda, const int period, const double s_thr) Line 197	                             C++
 	ffm1.exe!main() Line 254	                                            C++
 	ffm1.exe!__tmainCRTStartup() Line 536	                                    C
 	ffm1.exe!mainCRTStartup() Line 377	                                     C
 	kernel32.dll!75c233aa()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	ntdll.dll!77be9ef2()	                                         Unknown
 	ntdll.dll!77be9ec5()	                                         Unknown


Could anyone please tell me how to read this list and where to find my error? Is it the "main() line 254" the place that I should look?
I thought an image would be more helpful than a description.

http://postimage.org/image/uch0smgxn/

[edit: In the stack trace you posted, you want to look for the first function that is your code. That would appear to be Fshare.]
Last edited on
Thanks cire for your image. That does help. I tracked now the line where is the error: in the function Fshare, line 197. The code is as follows from this line:

1
2
3
4
5
6
7
	for(auto iter(v.begin()); iter != v.end()-nbe; iter++)  // line 197
	{
		if((*iter)->exit == 0)
		{
			sum_sp += *((*iter)->s.end()-1) / *((*iter)->p.end()-1);
		}
	}


v is the big vector that contents big number of objects. The code looks fine (for me). The question is, this exception is thrown 1 time out of 15~20 simulations. I am just confused.

What is your opinion about this? Should I modify the code to avoid an potential error somehow? Thanks a lot!


http://www.cplusplus.com/articles/EzywvCM9/


This shows an easier way to use range based for loops:

1
2
3
4
// the C++11 way
for (int &item: v);
// item will become, in order, all the things stored in v
// notice how we're referencing the item, that allows us to change it 


I am not sure whether you were aware of this already, and I have no idea whether this will have any impact on your problem. If not - then you are best being in the very good care of cire

Edit:

Here's another version:

for ( auto &item : TheContainerVariable ) {}
HTH
Last edited on
In for(auto iter(v.begin()); iter != v.end()-nbe; iter++) // line 197 , is it ever possible for nbe to be greater than v.size()? [edit: or negative since it's an int?]
Last edited on
Thanks TheIdeasMan for your tips. I really didn't know this new method. I will read the article you recommend me.

You are right cire, the error may come from "nbe". For 5% of probability its value could be -1 (its value is the result of a very long calculation). I can imagine that the iterator cannot accept a negative value here. So that is why it stopped sometimes. Am I right?

I will take a measure to prevent this from happening.

Many thanks to you guys, I really have learned a lot from your helps!
Last edited on
You are right cire, the error may come from "nbe". For 5% of probability its value could be -1 (its value is the result of a very long calculation). I can imagine that the iterator cannot accept a negative value here. So that is why it stopped sometimes. Am I right?


More or less. v.end() - -1 => v.end()+1 is an invalid iterator.

Range-based fors are very nice when you want to iterate through the entire container. That isn't the case here, though.
Thanks a lot cire! :)
Topic archived. No new replies allowed.