Index: src/TortoiseShell/PreserveChdir.cpp
===================================================================
--- src/TortoiseShell/PreserveChdir.cpp	(revision 19671)
+++ src/TortoiseShell/PreserveChdir.cpp	(working copy)
@@ -19,37 +19,42 @@
 #include "stdafx.h"
 #include "PreserveChdir.h"
 
-PreserveChdir::PreserveChdir()
+PreserveChdir::PreserveChdir() :
+    size(GetCurrentDirectory(0, NULL)),
+    originalCurrentDirectory(NULL)
 {
-	DWORD len = GetCurrentDirectory(0, NULL);
-	if (len)
-	{
-		originalCurrentDirectory = new TCHAR[len];
-		if (GetCurrentDirectory(len, originalCurrentDirectory)==0)
-		{
-			delete [] originalCurrentDirectory;
-			originalCurrentDirectory = NULL;
-		}
-	}
-	else
-		originalCurrentDirectory = NULL;
+	if (size == 0)
+		return;
+
+	originalCurrentDirectory = new TCHAR[size];
+	if (GetCurrentDirectory(size, originalCurrentDirectory)!=0)
+		return; // succeeded
+
+    // GetCurrentDirectory failed
+	delete [] originalCurrentDirectory;
+	originalCurrentDirectory = NULL;
 }
 
 PreserveChdir::~PreserveChdir()
 {
-	if (originalCurrentDirectory)
-	{
-		DWORD len = GetCurrentDirectory(0, NULL);
+    if (!originalCurrentDirectory)
+        return; // nothing to do
+
+	// _tchdir is an expensive function - don't call it unless we really have to
+	bool reset = true;
+	const DWORD len = GetCurrentDirectory(0, NULL);
+    if(len == size) {
+        // same size, must check contents
 		TCHAR * currentDirectory = new TCHAR[len];
-
-		// _tchdir is an expensive function - don't call it unless we really have to
-		GetCurrentDirectory(len, currentDirectory);
-		if(_tcscmp(currentDirectory, originalCurrentDirectory) != 0)
-		{
-			SetCurrentDirectory(originalCurrentDirectory);
-		}
+        if(GetCurrentDirectory(len, currentDirectory) != 0)
+			if(_tcscmp(currentDirectory, originalCurrentDirectory) == 0)
+				reset = false;	// no change required
 		delete [] currentDirectory;
-		delete [] originalCurrentDirectory;
-		originalCurrentDirectory = NULL;
 	}
+
+    // reset directory
+	if(reset)
+		SetCurrentDirectory(originalCurrentDirectory);
+	delete [] originalCurrentDirectory;
+	originalCurrentDirectory = NULL;
 }
Index: src/TortoiseShell/PreserveChdir.h
===================================================================
--- src/TortoiseShell/PreserveChdir.h	(revision 19671)
+++ src/TortoiseShell/PreserveChdir.h	(working copy)
@@ -31,10 +31,14 @@
 class PreserveChdir
 {
 public:
-	PreserveChdir();
-	~PreserveChdir();
+    PreserveChdir();                                ///< saves originalCurrentDirectory
+    ~PreserveChdir();                               ///< restores originalCurrentDirectory
 
 private:
-	TCHAR  * originalCurrentDirectory;
+    PreserveChdir(const PreserveChdir&);            ///< non-copyable
+    PreserveChdir& operator=(const PreserveChdir&); ///< non-assignable
+
+    const size_t size;                              ///< size of originalCurrentDirectory
+    TCHAR  * originalCurrentDirectory;			    ///< %CD% at ctor time
 };
 
