Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 41062)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 41063)
@@ -7,5 +7,5 @@
 
 /*
- * Copyright (C) 2010 Oracle Corporation
+ * Copyright (C) 2010-2012 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -18,8 +18,8 @@
  */
 
-/* Global includes */
+/* Global includes: */
 #include <QTimer>
 
-/* Local includes */
+/* Local includes: */
 #include "VBoxGlobal.h"
 #include "UIMachine.h"
@@ -28,16 +28,21 @@
 #include "UIMachineLogic.h"
 #include "UIMachineWindow.h"
-
 #ifdef Q_WS_MAC
 # include <ApplicationServices/ApplicationServices.h>
 #endif /* Q_WS_MAC */
 
+/* Visual state interface: */
 class UIVisualState : public QObject
 {
     Q_OBJECT;
 
+signals:
+
+    /* Signal to change-state: */
+    void sigChangeVisualState(UIVisualStateType newVisualStateType);
+
 public:
 
-    /* Visual state holder constructor: */
+    /* Constructor: */
     UIVisualState(QObject *pParent, UISession *pSession)
         : QObject(pParent)
@@ -52,8 +57,19 @@
     }
 
-    /* Public getters: */
-    UIMachineLogic* machineLogic() const  { return m_pMachineLogic; }
+    /* Destructor: */
+    ~UIVisualState()
+    {
+        /* Delete machine logic if exists: */
+        if (m_pMachineLogic)
+            UIMachineLogic::destroy(m_pMachineLogic);
+    }
+
+    /* Visual state type getter: */
     virtual UIVisualStateType visualStateType() const = 0;
 
+    /* Machine logic getter: */
+    UIMachineLogic* machineLogic() const { return m_pMachineLogic; }
+
+    /* Method to prepare change one visual state to another: */
     virtual bool prepareChange(UIVisualStateType previousVisualStateType)
     {
@@ -78,6 +94,8 @@
     }
 
+    /* Method to change one visual state to another: */
     virtual void change() = 0;
 
+    /* Method to finish change one visual state to another: */
     virtual void finishChange()
     {
@@ -95,13 +113,7 @@
     }
 
-
-signals:
-
-    /* Signal to change-state: */
-    void sigChangeVisualState(UIVisualStateType visualStateType);
-
 protected:
 
-    /* Protected members: */
+    /* Variables: */
     UISession *m_pSession;
     UIMachineLogic *m_pMachineLogic;
@@ -111,4 +123,5 @@
 };
 
+/* Normal visual state implementation: */
 class UIVisualStateNormal : public UIVisualState
 {
@@ -117,10 +130,21 @@
 public:
 
-    /* Normal visual state holder constructor: */
+    /* Constructor: */
     UIVisualStateNormal(QObject *pParent, UISession *pSession)
         : UIVisualState(pParent, pSession) {}
 
+private slots:
+
+    /* State-change handlers: */
+    void sltGoToFullscreenMode() { emit sigChangeVisualState(UIVisualStateType_Fullscreen); }
+    void sltGoToSeamlessMode() { emit sigChangeVisualState(UIVisualStateType_Seamless); }
+    void sltGoToScaleMode() { emit sigChangeVisualState(UIVisualStateType_Scale); }
+
+private:
+
+    /* Visual state type getter: */
     UIVisualStateType visualStateType() const { return UIVisualStateType_Normal; }
 
+    /* Method to change previous visual state to this one: */
     void change()
     {
@@ -136,26 +160,7 @@
         m_pMachineLogic->initialize();
     }
-
-private slots:
-
-    void sltGoToFullscreenMode()
-    {
-        /* Change visual state to fullscreen: */
-        emit sigChangeVisualState(UIVisualStateType_Fullscreen);
-    }
-
-    void sltGoToSeamlessMode()
-    {
-        /* Change visual state to seamless: */
-        emit sigChangeVisualState(UIVisualStateType_Seamless);
-    }
-
-    void sltGoToScaleMode()
-    {
-        /* Change visual state to scale: */
-        emit sigChangeVisualState(UIVisualStateType_Scale);
-    }
 };
 
+/* Fullscreen visual state implementation: */
 class UIVisualStateFullscreen : public UIVisualState
 {
@@ -164,5 +169,5 @@
 public:
 
-    /* Fullscreen visual state holder constructor: */
+    /* Constructor: */
     UIVisualStateFullscreen(QObject *pParent, UISession *pSession)
         : UIVisualState(pParent, pSession)
@@ -179,5 +184,5 @@
     }
 
-    /* Fullscreen visual state holder destructor: */
+    /* Destructor: */
     virtual ~UIVisualStateFullscreen()
     {
@@ -193,6 +198,17 @@
     }
 
+private slots:
+
+    /* State-change handlers: */
+    void sltGoToNormalMode() { emit sigChangeVisualState(UIVisualStateType_Normal); }
+    void sltGoToSeamlessMode() { emit sigChangeVisualState(UIVisualStateType_Seamless); }
+    void sltGoToScaleMode() { emit sigChangeVisualState(UIVisualStateType_Scale); }
+
+private:
+
+    /* Visual state type getter: */
     UIVisualStateType visualStateType() const { return UIVisualStateType_Fullscreen; }
 
+    /* Method to change previous visual state to this one: */
     void change()
     {
@@ -208,26 +224,7 @@
         m_pMachineLogic->initialize();
     }
-
-private slots:
-
-    void sltGoToNormalMode()
-    {
-        /* Change visual state to normal: */
-        emit sigChangeVisualState(UIVisualStateType_Normal);
-    }
-
-    void sltGoToSeamlessMode()
-    {
-        /* Change visual state to seamless: */
-        emit sigChangeVisualState(UIVisualStateType_Seamless);
-    }
-
-    void sltGoToScaleMode()
-    {
-        /* Change visual state to scale: */
-        emit sigChangeVisualState(UIVisualStateType_Scale);
-    }
 };
 
+/* Seamless visual state implementation: */
 class UIVisualStateSeamless : public UIVisualState
 {
@@ -236,5 +233,5 @@
 public:
 
-    /* Seamless visual state holder constructor: */
+    /* Constructor: */
     UIVisualStateSeamless(QObject *pParent, UISession *pSession)
         : UIVisualState(pParent, pSession)
@@ -251,5 +248,5 @@
     }
 
-    /* Seamless visual state holder destructor: */
+    /* Destructor: */
     virtual ~UIVisualStateSeamless()
     {
@@ -265,6 +262,17 @@
     }
 
+private slots:
+
+    /* State-change handlers: */
+    void sltGoToNormalMode() { emit sigChangeVisualState(UIVisualStateType_Normal); }
+    void sltGoToFullscreenMode() { emit sigChangeVisualState(UIVisualStateType_Fullscreen); }
+    void sltGoToScaleMode() { emit sigChangeVisualState(UIVisualStateType_Scale); }
+
+private:
+
+    /* Visual state type getter: */
     UIVisualStateType visualStateType() const { return UIVisualStateType_Seamless; }
 
+    /* Method to change previous visual state to this one: */
     void change()
     {
@@ -280,26 +288,7 @@
         m_pMachineLogic->initialize();
     }
-
-private slots:
-
-    void sltGoToNormalMode()
-    {
-        /* Change visual state to normal: */
-        emit sigChangeVisualState(UIVisualStateType_Normal);
-    }
-
-    void sltGoToFullscreenMode()
-    {
-        /* Change visual state to fullscreen: */
-        emit sigChangeVisualState(UIVisualStateType_Fullscreen);
-    }
-
-    void sltGoToScaleMode()
-    {
-        /* Change visual state to scale: */
-        emit sigChangeVisualState(UIVisualStateType_Scale);
-    }
 };
 
+/* Scale visual state implementation: */
 class UIVisualStateScale : public UIVisualState
 {
@@ -308,5 +297,5 @@
 public:
 
-    /* Scale visual state holder constructor: */
+    /* Constructor: */
     UIVisualStateScale(QObject *pParent, UISession *pSession)
         : UIVisualState(pParent, pSession)
@@ -323,5 +312,5 @@
     }
 
-    /* Seamless visual state holder destructor: */
+    /* Destructor: */
     virtual ~UIVisualStateScale()
     {
@@ -337,6 +326,17 @@
     }
 
+private slots:
+
+    /* State-change handlers: */
+    void sltGoToNormalMode() { emit sigChangeVisualState(UIVisualStateType_Normal); }
+    void sltGoToFullscreenMode() { emit sigChangeVisualState(UIVisualStateType_Fullscreen); }
+    void sltGoToSeamlessMode() { emit sigChangeVisualState(UIVisualStateType_Seamless); }
+
+private:
+
+    /* Visual state type getter: */
     UIVisualStateType visualStateType() const { return UIVisualStateType_Scale; }
 
+    /* Method to change previous visual state to this one: */
     void change()
     {
@@ -352,24 +352,4 @@
         m_pMachineLogic->initialize();
     }
-
-private slots:
-
-    void sltGoToNormalMode()
-    {
-        /* Change visual state to normal: */
-        emit sigChangeVisualState(UIVisualStateType_Normal);
-    }
-
-    void sltGoToFullscreenMode()
-    {
-        /* Change visual state to fullscreen: */
-        emit sigChangeVisualState(UIVisualStateType_Fullscreen);
-    }
-
-    void sltGoToSeamlessMode()
-    {
-        /* Change visual state to seamless: */
-        emit sigChangeVisualState(UIVisualStateType_Seamless);
-    }
 };
 
@@ -436,9 +416,9 @@
 }
 
-void UIMachine::sltChangeVisualState(UIVisualStateType visualStateType)
+void UIMachine::sltChangeVisualState(UIVisualStateType newVisualStateType)
 {
     /* Create new state: */
     UIVisualState *pNewVisualState = 0;
-    switch (visualStateType)
+    switch (newVisualStateType)
     {
         case UIVisualStateType_Normal:
@@ -470,4 +450,5 @@
     }
 
+    /* Get previous visual state type: */
     UIVisualStateType previousVisualStateType = UIVisualStateType_Normal;
     if (m_pVisualState)
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 41062)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 41063)
@@ -7,5 +7,5 @@
 
 /*
- * Copyright (C) 2010-2011 Oracle Corporation
+ * Copyright (C) 2010-2012 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -105,4 +105,5 @@
 Q_DECLARE_METATYPE(USBTarget);
 
+/* static */
 UIMachineLogic* UIMachineLogic::create(QObject *pParent,
                                        UISession *pSession,
@@ -126,4 +127,10 @@
     }
     return logic;
+}
+
+/* static */
+void UIMachineLogic::destroy(UIMachineLogic *pWhichLogic)
+{
+    delete pWhichLogic;
 }
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h	(revision 41062)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.h	(revision 41063)
@@ -6,5 +6,5 @@
 
 /*
- * Copyright (C) 2010-2011 Oracle Corporation
+ * Copyright (C) 2010-2012 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -50,8 +50,9 @@
 public:
 
-    /* Factory function to create required logic sub-child: */
+    /* Factory functions to create/destroy required logic sub-child: */
     static UIMachineLogic* create(QObject *pParent,
                                   UISession *pSession,
                                   UIVisualStateType visualStateType);
+    static void destroy(UIMachineLogic *pWhichLogic);
 
     /* Check if this mode is available: */
