Fix file selector crash

Sometimes a var change event came in while the file selector was
being rendered. Changes to the mFolderList or mFileList vectors
sometimes resulted in a crash. This patch set moves the
GetFileList call to the Render function to prevent the vectors
from changing while the render is taking place.
diff --git a/gui/fileselector.cpp b/gui/fileselector.cpp
index edf3279..4da72d8 100644
--- a/gui/fileselector.cpp
+++ b/gui/fileselector.cpp
@@ -69,6 +69,7 @@
 	hasHighlightColor = false;
 	hasFontHighlightColor = false;
 	isHighlighted = false;
+	updateFileList = false;
 	startSelection = -1;
 
 	// Load header text
@@ -377,6 +378,13 @@
 
 int GUIFileSelector::Render(void)
 {
+	// Update the file list if needed
+	if (updateFileList) {
+		string value;
+		DataManager::GetValue(mPathVar, value);
+		GetFileList(value);
+		updateFileList = false;
+	}
 	// First step, fill background
 	gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
 	gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH);
@@ -789,7 +797,6 @@
 					else
 					{
 						DataManager::SetValue(mPathVar, cwd);
-						GetFileList(cwd);
 						mStart = 0;
 						scrollingY = 0;
 						mUpdate = 1;
@@ -824,11 +831,6 @@
 
 int GUIFileSelector::NotifyVarChange(std::string varName, std::string value)
 {
-	if (varName.empty())
-	{
-		// Always clear the data variable so we know to use it
-		DataManager::SetValue(mVariable, "");
-	}
 	if (!mHeaderIsStatic) {
 		std::string newValue = gui_parse_text(mHeaderText);
 		if (mLastValue != newValue) {
@@ -841,12 +843,14 @@
 	}
 	if (varName == mPathVar || varName == mSortVariable)
 	{
-		DataManager::GetValue(mPathVar, value);  // sometimes the value will be the sort order instead of the path, so we read the path everytime
-		DataManager::GetValue(mSortVariable, mSortOrder);
+		// If needed, wait for render to finish before continuing or the list change may not register
+		while (updateFileList || mUpdate) {
+			usleep(500);
+		}
+		updateFileList = true;
 		mStart = 0;
 		scrollingY = 0;
 		scrollingSpeed = 0;
-		GetFileList(value);
 		mUpdate = 1;
 		return 0;
 	}
@@ -976,6 +980,7 @@
 
 	std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
 	std::sort(mFileList.begin(), mFileList.end(), fileSort);
+
 	return 0;
 }
 
@@ -983,9 +988,9 @@
 {
 	if (inFocus)
 	{
-		std::string value;
-		DataManager::GetValue(mPathVar, value);
-		GetFileList(value);
+		updateFileList = true;
+		scrollingY = 0;
+		scrollingSpeed = 0;
+		mUpdate = 1;
 	}
 }
-
diff --git a/gui/objects.hpp b/gui/objects.hpp
index 8e0cc94..3a39fe5 100644
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -495,6 +495,7 @@
 	COLOR mHighlightColor;
 	COLOR mFontHighlightColor;
 	int startSelection;
+	bool updateFileList;
 };
 
 class GUIListBox : public RenderObject, public ActionObject