Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 52999)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 53000)
@@ -32,4 +32,5 @@
 /* COM includes: */
 # include "CMachine.h"
+# include "CSession.h"
 # include "CConsole.h"
 # include "CSnapshot.h"
@@ -150,8 +151,26 @@
     }
 
+    /* Try to create machine UI: */
+    return create();
+}
+
+/* static */
+bool UIMachine::create()
+{
+    /* Make sure machine is null pointer: */
+    AssertReturn(m_spInstance == 0, false);
+
     /* Create machine UI: */
     new UIMachine;
-    /* Prepare machine UI: */
-    return m_spInstance->prepare();
+    /* Make sure it's prepared: */
+    if (!m_spInstance->prepare())
+    {
+        /* Destroy machine UI otherwise: */
+        destroy();
+        /* False in that case: */
+        return false;
+    }
+    /* True by default: */
+    return true;
 }
 
@@ -159,4 +178,7 @@
 void UIMachine::destroy()
 {
+    /* Make sure machine is valid pointer: */
+    AssertReturnVoid(m_spInstance != 0);
+
     /* Cleanup machine UI: */
     m_spInstance->cleanup();
@@ -229,12 +251,7 @@
 bool UIMachine::prepare()
 {
-    /* Create VM session: */
-    m_session = vboxGlobal().openSession(vboxGlobal().managedVMUuid(),
-                                         vboxGlobal().isSeparateProcess() ? KLockType_Shared : KLockType_VM);
-    if (m_session.isNull())
+    /* Try to create session UI: */
+    if (!UISession::create(m_pSession, this))
         return false;
-
-    /* Create UI session: */
-    m_pSession = new UISession(this, m_session);
 
     /* Preventing application from closing in case of window(s) closed: */
@@ -272,4 +289,5 @@
 {
     /* Load 'visual state' option: */
+    if (uisession())
     {
         /* Load restricted visual states: */
@@ -299,4 +317,5 @@
 {
     /* Save 'visual state' option: */
+    if (uisession())
     {
         /* Get requested visual state: */
@@ -324,11 +343,7 @@
     m_pVisualState = 0;
 
-    /* Delete UI session: */
-    delete m_pSession;
-    m_pSession = 0;
-
-    /* Free session finally: */
-    m_session.UnlockMachine();
-    m_session.detach();
+    /* Destroy session UI if necessary: */
+    if (uisession())
+        UISession::destroy(m_pSession);
 
     /* Quit application: */
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 52999)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 53000)
@@ -27,5 +27,4 @@
 /* COM includes: */
 #include "COMEnums.h"
-#include "CSession.h"
 
 /* Forward declarations: */
@@ -51,4 +50,6 @@
       * @return true if machine was started, false otherwise. */
     static bool startMachine(const QString &strID);
+    /** Static constructor. */
+    static bool create();
     /** Static destructor. */
     static void destroy();
@@ -96,7 +97,4 @@
     static UIMachine* m_spInstance;
 
-    /** Holds the session instance. */
-    CSession m_session;
-
     /** Holds the session UI instance. */
     UISession *m_pSession;
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 52999)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 53000)
@@ -128,9 +128,41 @@
 #endif /* Q_WS_MAC */
 
-UISession::UISession(UIMachine *pMachine, CSession &sessionReference)
+/* static */
+bool UISession::create(UISession *&pSession, UIMachine *pMachine)
+{
+    /* Make sure null pointer passed: */
+    AssertReturn(pSession == 0, false);
+
+    /* Create session UI: */
+    pSession = new UISession(pMachine);
+    /* Make sure it's prepared: */
+    if (!pSession->prepare())
+    {
+        /* Destroy session UI otherwise: */
+        destroy(pSession);
+        /* False in that case: */
+        return false;
+    }
+    /* True by default: */
+    return true;
+}
+
+/* static */
+void UISession::destroy(UISession *&pSession)
+{
+    /* Make sure valid pointer passed: */
+    AssertReturnVoid(pSession != 0);
+
+    /* Cleanup session UI: */
+    pSession->cleanup();
+    /* Destroy session: */
+    delete pSession;
+    pSession = 0;
+}
+
+UISession::UISession(UIMachine *pMachine)
     : QObject(pMachine)
     /* Base variables: */
     , m_pMachine(pMachine)
-    , m_session(sessionReference)
     , m_pActionPool(0)
 #ifdef Q_WS_MAC
@@ -139,5 +171,5 @@
     /* Common variables: */
     , m_machineStatePrevious(KMachineState_Null)
-    , m_machineState(session().GetMachine().GetState())
+    , m_machineState(KMachineState_Null)
 #ifndef Q_WS_MAC
     , m_pMachineWindowIcon(0)
@@ -181,4 +213,12 @@
     , m_fIsHidingHostPointer(true)
 {
+}
+
+bool UISession::prepare()
+{
+    /* Prepare session: */
+    if (!prepareSession())
+        return false;
+
     /* Prepare actions: */
     prepareActions();
@@ -206,7 +246,14 @@
     sigaction(SIGUSR1, &sa, NULL);
 #endif /* VBOX_GUI_WITH_KEYS_RESET_HANDLER */
+
+    /* True by default: */
+    return true;
 }
 
 UISession::~UISession()
+{
+}
+
+void UISession::cleanup()
 {
 #ifdef Q_WS_WIN
@@ -230,4 +277,7 @@
     /* Cleanup actions: */
     cleanupActions();
+
+    /* Cleanup session: */
+    cleanupSession();
 }
 
@@ -966,4 +1016,20 @@
 }
 
+bool UISession::prepareSession()
+{
+    /* Open session: */
+    m_session = vboxGlobal().openSession(vboxGlobal().managedVMUuid(),
+                                         vboxGlobal().isSeparateProcess()
+                                         ? KLockType_Shared : KLockType_VM);
+    if (m_session.isNull())
+        return false;
+
+    /* Update machine-state: */
+    m_machineState = m_session.GetMachine().GetState();
+
+    /* True by default: */
+    return true;
+}
+
 void UISession::prepareConsoleEventHandlers()
 {
@@ -1325,4 +1391,14 @@
 }
 
+void UISession::cleanupSession()
+{
+    /* Close session: */
+    if (!m_session.isNull())
+    {
+        m_session.UnlockMachine();
+        m_session.detach();
+    }
+}
+
 #ifdef Q_WS_MAC
 void UISession::updateMenu()
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 52999)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 53000)
@@ -29,4 +29,5 @@
 /* COM includes: */
 #include "COMEnums.h"
+#include "CSession.h"
 
 /* Forward declarations: */
@@ -36,5 +37,4 @@
 class UIMachineLogic;
 class UIActionPool;
-class CSession;
 class CUSBDevice;
 class CNetworkAdapter;
@@ -77,7 +77,8 @@
 public:
 
-    /* Machine uisession constructor/destructor: */
-    UISession(UIMachine *pMachine, CSession &session);
-    virtual ~UISession();
+    /** Factory constructor. */
+    static bool create(UISession *&pSession, UIMachine *pMachine);
+    /** Factory destructor. */
+    static void destroy(UISession *&pSession);
 
     /* API: Runtime UI stuff: */
@@ -309,8 +310,15 @@
 private:
 
+    /** Constructor. */
+    UISession(UIMachine *pMachine);
+    /** Destructor. */
+    ~UISession();
+
     /* Private getters: */
     UIMachine* uimachine() const { return m_pMachine; }
 
     /* Prepare helpers: */
+    bool prepare();
+    bool prepareSession();
     void prepareActions();
     void prepareConnections();
@@ -327,4 +335,6 @@
     void cleanupConnections();
     void cleanupActions();
+    void cleanupSession();
+    void cleanup();
 
 #ifdef Q_WS_MAC
@@ -344,5 +354,7 @@
     /* Private variables: */
     UIMachine *m_pMachine;
-    CSession &m_session;
+
+    /** Holds the session instance. */
+    CSession m_session;
 
     /** Holds the action-pool instance. */
