terminate called after throwing an instance of 'std::out_of_range'

Hello everyone
I'm new to C++ coding and i just registered on the site (nice forums!)

I started c++ 2 months ago but i'm a very adaptive person.
So anyway... The program i've compiled gives an error randomly sometimes and terminates the program :/

i've managed to track down the source of it with logging.

Here are the possible causes:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted


Code:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
	for( vector<CCallableManualQuery *> :: iterator i = d_ManualQueries.begin( ); i != d_ManualQueries.end( ); )
	{
		DEV_Print("Recovering callables...");
		if( (*i)->GetReady( ) )
		{
			DEV_Print("Attempting to recover and erase.");
			m_DB->RecoverCallable( *i );
			delete *i;
			m_PendingQueries--;
			DEV_Print("erasing callable..");
			i = d_ManualQueries.erase( i );
			DEV_Print("done erasing! :D");
		}
		else
			i++;
	}

	for( vector<string> :: iterator i = d_DelayedQueries.begin( ); i != d_DelayedQueries.end( ); )
	{
		DEV_Print("Doing loop.");
		if(GetTicks( ) - m_QueryTick >= 200)
		{
			DEV_Print("query on time. executing.");
			d_ManualQueries.push_back( m_DB->ThreadedManualQuery( *i ) );
			DEV_Print("added to thread and will be executed. Erasing.");
            try {
	    		i = d_DelayedQueries.erase( i );
            }
            catch (out_of_range &e) {
				cout << "Out of range: " << e.what( ) << "\n";
            }
			DEV_Print("erased.");
			m_QueryTick = GetTicks( );
			DEV_Print("Breaking loop.");
			return false;
		}
		else
			i++;
	}



DEV_Print( ) is a function that logs the output to a text file.

d_ManualQueries is a vector<CCallableManualQuery *>
CCallableManualQuery is a class that executes a query to another thread.

IF you need more info please reply!
Thanks!
Well. The error is... exactly what it says on the tin, lol. You're trying to erase something that's out of the bounds of your string.

Check line 11. There's an erase() there that's not in a try-catch block. Try stuffing it in one of those blocks and see if anything changes...

-Albatross

P.S.- Two months? Not bad.
Last edited on
thanks, i'll try doing it.

But i still don't understand :P
From what a logic mind comprehends, this is not normal!

EDIT:

Meh :/

Here's what the Print says:

http://pastebin.com/sDrhFHds

EDIT:

And btw, when i try to compile for linux (g++ ubuntu)
it won't compile with those try/catch
gives a lot of errors :/
Last edited on
Wait a sec... /desk

Idiot me. None of the erase()s in this snippet erase anything in a string... do you use std::string::erase() anywhere in your program and if so where?

-Albatross
Last edited on
Hi Albatross

I never erase a string variable
i only use .erase( ) for global vector variables

and like i told you, with the DEV_Print( ) function, the problem only happens when i push a string into that vector. Here let me show you the function that does that

1
2
3
4
5
6
7
8
9
10
11
void CBaseGame :: SQLex( string q )
{
	if(q.empty( ))
		return;
	else if(q.size( ) < 2)
		return;

	m_GHost->m_PendingQueries++;
	CONSOLE_Print("Adding query to queue, [ " + UTIL_ToString( m_GHost->m_PendingQueries ) + " ] Total.");
	m_GHost->d_DelayedQueries.push_back( q );
}


it's in another class
after that message is shown on the console, the program crashes :/

EDIT:: The program is intended for UNIX (linux - ubuntu) OS
I haven't tested it on windows
Last edited on
Topic archived. No new replies allowed.