I'm attempting to escape chars in a wstring by iterating over the string and inserting an escape character in the appropriate location. The function below is preparing INSERT statements for SQL by adding a single quote to existing single quotes in the string. ( e.g. ( ' ) becomes ( '' ) ). The odd thing is that all string insertions work as expected except on a single case where the error is given. The string that's giving the error isn't special in any way. There's a single quote in the middle of the string and string insert updates it as expected. But, after the insert the iterator is broken and on the subsequent dereferences the error is given.


Debug Assertion Failed!
Expression: string iterator not dereferencable.


Code:
        wstring str( "Some string with a ' single quote in it." )
        wstring::iterator iter;
	for( iter = str.begin(); iter != str.end(); ++iter )
	{
		if( *iter == L'\'' )
		{	
			str.insert( iter++, L'\'' );			
		}
	}

Any help is very much appreciated.

Andrew