MUD-like text box wrapping, crashes program

My game has a text box, similar to that of MUD games. I’m trying to add a feature where the text will be wrapped according to the width of the text compared to the width of the window.

The problem is that when the window is resized to a width that is shorter than that of the line of text, it fails to render the surface.

I’ve added some commentary on the code to make it more understandable, hopefully it should be clear enough; if not, I would be more than happy to rectify any problem.

Here is my repository: https://gitlab.com/aumars/caventure

void TextMenu::Update(SDL_Renderer *renderer, TTF_Font *font, SDL_Color color, SDL_Rect output)
{
	/* Renders each line of text from the bottom up */
	for (unsigned int i = 0; i < mTextBoxVector.size(); i++)
	{
		/* Initialises variables */
		int length, temp, newlength, newtemp;
		std::vector<std::string> NewTextVector;

		/* Measures length of text from mTextBoxVector[i] */
		if(TTF_SizeText(Global.mFont, &mTextBoxVector[i][0u], &length, &temp))
		{
			throw(::std::runtime_error("TTF_SizeFont did not work!"));
		}

		/* Executes if text width from mTextBoxVector[i] is smaller than window width */
		if (length > Global.mWidth)
		{
			/* Adds an element (string) to NewTextVector */
			std::string str;
			NewTextVector.insert(NewTextVector.end(), str);

			/* Loops until length is smaller than window width */
			while (length > Global.mWidth)
			{
				/* Moves the last character of mTextBoxVector[i] to the end of element 0
				of NewTextVector */
				std::string::iterator it = mTextBoxVector[i].end();
				NewTextVector[0].push_back(*it);
				mTextBoxVector[i].pop_back();

				if(TTF_SizeText(Global.mFont, &mTextBoxVector[i][0u], &length, &newtemp))
				{
					throw(::std::runtime_error("TTF_SizeFont did not work!"));
				}
			}

			/* At this point, the text width of mTextBoxVector[i] is smaller than
			window width and NewTextVector[0] is of unknown width */

			unsigned int j = 0;

			if(TTF_SizeText(Global.mFont, &NewTextVector[j][0u], &newlength, &newtemp))
			{
				throw(::std::runtime_error("TTF_SizeFont did not work!"));
			}

			/* Loops until text width of all elements of NewTextVector is smaller
			than window width */
			while (newlength > Global.mWidth)
			{
				/* Adds an element (string) to NewTextVector */
				std::string str;
				NewTextVector.insert(NewTextVector.end(), str);

				/* Increments to the next element of NewTextVector */
				j++;

				while (newlength > Global.mWidth)
				{
					std::string::iterator it = NewTextVector[j - 1].end();
					NewTextVector[j].push_back(*it);
					NewTextVector[j - 1].pop_back();

					if(TTF_SizeText(Global.mFont, &NewTextVector[j - 1][0u], &newlength, &newtemp))
					{
						throw(::std::runtime_error("TTF_SizeFont did not work!"));
					}
				}

				/* If the NEXT element of NewTextVector is too long, run the loop again */
				if(TTF_SizeText(Global.mFont, &NewTextVector[j][0u], &newlength, &newtemp))
				{
					throw(::std::runtime_error("TTF_SizeFont did not work!"));
				}
			}
		}

		/* The text from mTextBoxVector[i] and NewTextVector should be now moved
		to mRawBoxesVector for rendering */

		RawTextBox box;
		box.mText = mTextBoxVector[i];
		mRawBoxesVector.insert(mRawBoxesVector.end(), box);

		for (unsigned int j = 0; j < NewTextVector.size(); j++)
		{
			RawTextBox box;
			box.mText = NewTextVector[j];
			mRawBoxesVector.insert(mRawBoxesVector.end(), box);
		}
	}

	/* By now, every string in mRawBoxesVector is optimised and ready to be
	rendered */

	/* Sets the clip SDL_Rect */
	SDL_RenderSetClipRect(renderer, &output);

	for (unsigned int i = 0; i < mRawBoxesVector.size(); i++)
	{
		/* Renders text onto one RawTextBox */
		mRawBoxesVector[i].Render(renderer, font, mRawBoxesVector[i].mText, color);

		/* Sets SDL_Rect for the text */
		SDL_Rect dstrect = { 0,
												 Global.mHeight - 40 - i * mRawBoxesVector[i].GetHeight(),
												 mRawBoxesVector[i].GetWidth(),
												 mRawBoxesVector[i].GetHeight() };

		/* Stacks dstrect into the renderer */
		SDL_RenderCopy(renderer, mRawBoxesVector[i].GetTexture(), NULL, &dstrect);
	}

mRawBoxesVector.clear();
}

/* Intiialises a new RawTextBox object with its text */
void TextMenu::NewBox(std::string text)
{
	/* Inserts the object into the front of boxes vector */
	mTextBoxVector.insert(mTextBoxVector.begin(), text);

	/* Deletes RawTextBoxes last on the vector in order to free space */
	if (mTextBoxVector.size() > 20)
	{
		mTextBoxVector.pop_back();
	}
}

Thanks in advance.