Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 30379)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 30380)
@@ -5095,15 +5095,14 @@
 
 /**
- * Tries to calculate the relative path of the given absolute path using the
- * directory of the machine settings file as the base directory.
- *
- * @param  aPath    Absolute path to calculate the relative path for.
- * @param  aResult  Where to put the result (used only when it's possible to
- *                  make a relative path from the given absolute path; otherwise
- *                  left untouched).
+ * Copies strSource to strTarget, making it relative to the machine folder
+ * if it is a subdirectory thereof, or simply copying it otherwise.
+ *
+ * @param strSource Path to evalue and copy.
+ * @param strTarget Buffer to receive target path.
  *
  * @note Locks this object for reading.
  */
-void Machine::calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult)
+void Machine::copyPathRelativeToMachine(const Utf8Str &strSource,
+                                        Utf8Str &strTarget)
 {
     AutoCaller autoCaller(this);
@@ -5113,17 +5112,13 @@
 
     AssertReturnVoid(!mData->m_strConfigFileFull.isEmpty());
-
-    Utf8Str settingsDir = mData->m_strConfigFileFull;
-
-    settingsDir.stripFilename();
-    if (RTPathStartsWith(strPath.c_str(), settingsDir.c_str()))
-    {
-        /* when assigning, we create a separate Utf8Str instance because both
-         * aPath and aResult can point to the same memory location when this
-         * func is called (if we just do aResult = aPath, aResult will be freed
-         * first, and since its the same as aPath, an attempt to copy garbage
-         * will be made. */
-        aResult = Utf8Str(strPath.c_str() + settingsDir.length() + 1);
-    }
+    // use strTarget as a temporary buffer to hold the machine settings dir
+    strTarget = mData->m_strConfigFileFull;
+    strTarget.stripFilename();
+    if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
+        // is relative: then append what's left
+        strTarget.append(strSource.c_str() + strTarget.length());     // include '/'
+    else
+        // is not relative: then overwrite
+        strTarget = strSource;
 }
 
@@ -7440,9 +7435,6 @@
             /* update m_strConfigFileFull amd mConfigFile */
             mData->m_strConfigFileFull = newConfigFile;
-
             // compute the relative path too
-            Utf8Str path = newConfigFile;
-            mParent->calculateRelativePath(path, path);
-            mData->m_strConfigFile = path;
+            mParent->copyPathRelativeToConfig(newConfigFile, mData->m_strConfigFile);
 
             // store the old and new so that VirtualBox::saveSettings() can update
@@ -7458,5 +7450,5 @@
 
             /* update the snapshot folder */
-            path = mUserData->mSnapshotFolderFull;
+            Utf8Str path = mUserData->mSnapshotFolderFull;
             if (RTPathStartsWith(path.c_str(), configDir.c_str()))
             {
@@ -7464,6 +7456,7 @@
                                   path.raw() + configDir.length());
                 mUserData->mSnapshotFolderFull = path;
-                calculateRelativePath(path, path);
-                mUserData->mSnapshotFolder = path;
+                Utf8Str strTemp;
+                copyPathRelativeToMachine(path, strTemp);
+                mUserData->mSnapshotFolder = strTemp;
             }
 
@@ -7720,8 +7713,5 @@
         Assert(!mSSData->mStateFilePath.isEmpty());
         /* try to make the file name relative to the settings file dir */
-        calculateRelativePath(mSSData->mStateFilePath, config.strStateFile);
-        if (!config.strStateFile.length())
-            // path is not relative (e.g. because snapshot folder was changed to a non-default location):
-            config.strStateFile = mSSData->mStateFilePath;
+        copyPathRelativeToMachine(mSSData->mStateFilePath, config.strStateFile);
     }
     else
@@ -8152,5 +8142,5 @@
             if (!mSSData->mStateFilePath.isEmpty())
                 /* try to make the file name relative to the settings file dir */
-                calculateRelativePath(mSSData->mStateFilePath, mData->pMachineConfigFile->strStateFile);
+                copyPathRelativeToMachine(mSSData->mStateFilePath, mData->pMachineConfigFile->strStateFile);
             else
                 mData->pMachineConfigFile->strStateFile.setNull();
Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30379)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30380)
@@ -2866,7 +2866,8 @@
                                      aNewPath,
                                      pcszMediumPath + strlen(aOldPath));
-        Utf8Str path = newPath;
-        m->pVirtualBox->calculateRelativePath(path, path);
         unconst(m->strLocationFull) = newPath;
+
+        Utf8Str path;
+        m->pVirtualBox->copyPathRelativeToConfig(newPath, path);
         unconst(m->strLocation) = path;
 
Index: /trunk/src/VBox/Main/SnapshotImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 30379)
+++ /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 30380)
@@ -762,6 +762,5 @@
     /* stateFile (optional) */
     if (!stateFilePath().isEmpty())
-        /* try to make the file name relative to the settings file dir */
-        m->pMachine->calculateRelativePath(stateFilePath(), data.strStateFile);
+        m->pMachine->copyPathRelativeToMachine(stateFilePath(), data.strStateFile);
     else
         data.strStateFile.setNull();
Index: /trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30379)
+++ /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30380)
@@ -3395,32 +3395,26 @@
 
 /**
- * Tries to calculate the relative path of the given absolute path using the
- * directory of the VirtualBox settings file as the base directory.
- *
- * @param  aPath    Absolute path to calculate the relative path for.
- * @param  aResult  Where to put the result (used only when it's possible to
- *                  make a relative path from the given absolute path; otherwise
- *                  left untouched).
- *
- * @note Doesn't lock any object.
- */
-void VirtualBox::calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult)
+ * Copies strSource to strTarget, making it relative to the VirtualBox config folder
+ * if it is a subdirectory thereof, or simply copying it otherwise.
+ *
+ * @param strSource Path to evalue and copy.
+ * @param strTarget Buffer to receive target path.
+ */
+void VirtualBox::copyPathRelativeToConfig(const Utf8Str &strSource,
+                                          Utf8Str &strTarget)
 {
     AutoCaller autoCaller(this);
     AssertComRCReturnVoid(autoCaller.rc());
 
-    /* no need to lock since mHomeDir is const */
-
-    Utf8Str settingsDir = m->strHomeDir;
-
-    if (RTPathStartsWith(strPath.c_str(), settingsDir.c_str()))
-    {
-        /* when assigning, we create a separate Utf8Str instance because both
-         * aPath and aResult can point to the same memory location when this
-         * func is called (if we just do aResult = aPath, aResult will be freed
-         * first, and since its the same as aPath, an attempt to copy garbage
-         * will be made. */
-        aResult = Utf8Str(strPath.c_str() + settingsDir.length() + 1);
-    }
+    // no need to lock since mHomeDir is const
+
+    // use strTarget as a temporary buffer to hold the machine settings dir
+    strTarget = m->strHomeDir;
+    if (RTPathStartsWith(strSource.c_str(), strTarget.c_str()))
+        // is relative: then append what's left
+        strTarget.append(strSource.c_str() + strTarget.length());     // include '/'
+    else
+        // is not relative: then overwrite
+        strTarget = strSource;
 }
 
Index: /trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MachineImpl.h	(revision 30379)
+++ /trunk/src/VBox/Main/include/MachineImpl.h	(revision 30380)
@@ -615,5 +615,5 @@
 
     int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
-    void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
+    void copyPathRelativeToMachine(const Utf8Str &strSource, Utf8Str &strTarget);
 
     void getLogFolder(Utf8Str &aLogFolder);
Index: /trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 30379)
+++ /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 30380)
@@ -282,5 +282,5 @@
 
     int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
-    void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
+    void copyPathRelativeToConfig(const Utf8Str &strSource, Utf8Str &strTarget);
 
     HRESULT registerHardDisk(Medium *aHardDisk, bool *pfNeedsSaveSettings);
