Index: trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp	(revision 52086)
@@ -4147,7 +4147,7 @@
 }
 
-VBoxQGLOverlay::VBoxQGLOverlay (QWidget *pViewport,QObject *pPostEventObject,  CSession * aSession, uint32_t id)
+VBoxQGLOverlay::VBoxQGLOverlay ()
     : mpOverlayWgt (NULL),
-      mpViewport (pViewport),
+      mpViewport (NULL),
       mGlOn (false),
       mOverlayWidgetVisible (false),
@@ -4157,11 +4157,20 @@
       mNeedOverlayRepaint (false),
       mNeedSetVisible (false),
-      mCmdPipe (pPostEventObject),
-      mSettings (*aSession),
-      mpSession(aSession),
+      mCmdPipe (),
+      mSettings (),
+      mpSession(),
       mpShareWgt (NULL),
-      m_id(id)
+      m_id(0)
 {
     /* postpone the gl widget initialization to avoid conflict with 3D on Mac */
+}
+
+void VBoxQGLOverlay::init(QWidget *pViewport, QObject *pPostEventObject,  CSession * aSession, uint32_t id)
+{
+    mpViewport = pViewport;
+    mpSession = aSession;
+    m_id = id;
+    mSettings.init(*aSession);
+    mCmdPipe.init(pPostEventObject);
 }
 
@@ -4948,6 +4957,6 @@
 }
 
-VBoxVHWACommandElementProcessor::VBoxVHWACommandElementProcessor(QObject *pNotifyObject) :
-    m_pNotifyObject(pNotifyObject),
+VBoxVHWACommandElementProcessor::VBoxVHWACommandElementProcessor() :
+    m_pNotifyObject(NULL),
     mpCurCmd(NULL),
     mbResetting(false),
@@ -4960,4 +4969,9 @@
 
     m_pCmdEntryCache = new VBoxVHWAEntriesCache;
+}
+
+void VBoxVHWACommandElementProcessor::init(QObject *pNotifyObject)
+{
+    m_pNotifyObject = pNotifyObject;
 }
 
@@ -5775,5 +5789,9 @@
 }
 
-VBoxVHWASettings::VBoxVHWASettings (CSession &session)
+VBoxVHWASettings::VBoxVHWASettings ()
+{
+}
+
+void VBoxVHWASettings::init(CSession &session)
 {
     const QString strMachineID = session.GetMachine().GetId();
Index: trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h	(revision 52086)
@@ -32,4 +32,6 @@
 #include "UIDefs.h"
 #include "VBoxFBOverlayCommon.h"
+#include "runtime/UIFrameBuffer.h"
+#include "runtime/UIMachineView.h"
 
 /* COM includes: */
@@ -84,5 +86,6 @@
 {
 public:
-    VBoxVHWASettings (CSession &session);
+    VBoxVHWASettings ();
+    void init(CSession &session);
 
     int fourccEnabledCount() const { return mFourccEnabledCount; }
@@ -1311,5 +1314,6 @@
 {
 public:
-    VBoxVHWACommandElementProcessor(QObject *pNotifyObject);
+    VBoxVHWACommandElementProcessor();
+    void init(QObject *pNotifyObject);
     ~VBoxVHWACommandElementProcessor();
     void postCmd(VBOXVHWA_PIPECMD_TYPE aType, void * pvData);
@@ -1673,5 +1677,6 @@
 {
 public:
-    VBoxQGLOverlay(QWidget *pViewport, QObject *pPostEventObject, CSession * aSession, uint32_t id);
+    VBoxQGLOverlay();
+    void init(QWidget *pViewport, QObject *pPostEventObject, CSession * aSession, uint32_t id);
     ~VBoxQGLOverlay()
     {
@@ -1819,17 +1824,19 @@
 };
 
-/* this additional class V is to workaround the [VBox|UI] duplication,
- * @todo: remove it once VBox stuff is removed */
-template <class T, class V>
-class VBoxOverlayFrameBuffer : public T
-{
-public:
-    VBoxOverlayFrameBuffer (V *pView, CSession * aSession, uint32_t id)
-        : T (pView),
-          mOverlay(pView->viewport(), pView, aSession, id),
-          mpView (pView)
-    {
+class VBoxOverlayFrameBuffer : public UIFrameBuffer
+{
+public:
+    VBoxOverlayFrameBuffer()
+    {
+    }
+
+    HRESULT init(UIMachineView *pView, CSession * aSession, uint32_t id)
+    {
+        mpView = pView;
+        UIFrameBuffer::init(mpView);
+        mOverlay.init(mpView->viewport(), mpView, aSession, id),
         /* sync with framebuffer */
         mOverlay.onResizeEventPostprocess (VBoxFBSizeInfo(this), QPoint(mpView->contentsX(), mpView->contentsY()));
+        return S_OK;
     }
 
@@ -1837,16 +1844,16 @@
     {
         int rc;
-        T::lock();
+        UIFrameBuffer::lock();
         /* Make sure frame-buffer is used: */
-        if (T::m_fUnused)
+        if (UIFrameBuffer::m_fUnused)
         {
             LogRel2(("ProcessVHWACommand: Postponed!\n"));
             /* Unlock access to frame-buffer: */
-            T::unlock();
+            UIFrameBuffer::unlock();
             /* tell client to pend ProcessVHWACommand */
             return E_ACCESSDENIED;
         }
         rc = mOverlay.onVHWACommand ((struct VBOXVHWACMD*)pCommand);
-        T::unlock();
+        UIFrameBuffer::unlock();
         if (rc == VINF_CALLBACK_RETURN)
             return S_OK;
@@ -1867,12 +1874,12 @@
     {
         HRESULT hr = S_OK;
-        T::lock();
+        UIFrameBuffer::lock();
         /* Make sure frame-buffer is used: */
-        if (T::m_fUnused)
+        if (UIFrameBuffer::m_fUnused)
         {
             LogRel2(("NotifyUpdate: Ignored!\n"));
             mOverlay.onNotifyUpdateIgnore (aX, aY, aW, aH);
             /* Unlock access to frame-buffer: */
-            T::unlock();
+            UIFrameBuffer::unlock();
             /*can we actually ignore the notify update?*/
             /* Ignore NotifyUpdate: */
@@ -1881,6 +1888,6 @@
 
         if (!mOverlay.onNotifyUpdate (aX, aY, aW, aH))
-            hr = T::NotifyUpdate (aX, aY, aW, aH);
-        T::unlock();
+            hr = UIFrameBuffer::NotifyUpdate (aX, aY, aW, aH);
+        UIFrameBuffer::unlock();
         return hr;
     }
@@ -1888,5 +1895,5 @@
     void resizeEvent(int iWidth, int iHeight)
     {
-        T::resizeEvent(iWidth, iHeight);
+        UIFrameBuffer::resizeEvent(iWidth, iHeight);
         mOverlay.onResizeEventPostprocess(VBoxFBSizeInfo(this),
                 QPoint(mpView->contentsX(), mpView->contentsY()));
@@ -1896,5 +1903,5 @@
     {
         mOverlay.onViewportResized (re);
-        T::viewportResized (re);
+        UIFrameBuffer::viewportResized (re);
     }
 
@@ -1902,20 +1909,20 @@
     {
         mOverlay.onViewportScrolled (QPoint(mpView->contentsX(), mpView->contentsY()));
-        T::viewportScrolled (dx, dy);
-    }
-
-    void setView(V * pView)
+        UIFrameBuffer::viewportScrolled (dx, dy);
+    }
+
+    void setView(UIMachineView * pView)
     {
         /* lock to ensure we do not collide with the EMT thread passing commands to us */
-        T::lock();
-        T::setView(pView);
+        UIFrameBuffer::lock();
+        UIFrameBuffer::setView(pView);
         mpView = pView;
         mOverlay.updateAttachment(pView ? pView->viewport() : NULL, pView);
-        T::unlock();
+        UIFrameBuffer::unlock();
     }
 
 private:
     VBoxQGLOverlay mOverlay;
-    V *mpView;
+    UIMachineView *mpView;
 };
 
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp	(revision 52086)
@@ -47,19 +47,22 @@
 #endif /* !Q_WS_WIN */
 
-UIFrameBuffer::UIFrameBuffer(UIMachineView *pMachineView)
+UIFrameBuffer::UIFrameBuffer()
     : m_iWidth(0), m_iHeight(0)
-    , m_pMachineView(pMachineView)
+    , m_pMachineView(NULL)
     , m_iWinId(0)
     , m_fUpdatesAllowed(true)
     , m_fUnused(false)
     , m_fAutoEnabled(false)
-#ifdef Q_OS_WIN
-    , m_iRefCnt(0)
-#endif /* Q_OS_WIN */
     , m_hiDPIOptimizationType(HiDPIOptimizationType_None)
     , m_dBackingScaleFactor(1.0)
 {
+}
+
+HRESULT UIFrameBuffer::init(UIMachineView *pMachineView)
+{
+    LogRel2(("UIFrameBuffer::init %p\n", this));
+
     /* Assign mahine-view: */
-    AssertMsg(m_pMachineView, ("UIMachineView must not be NULL\n"));
+    m_pMachineView = pMachineView;
     /* Cache window ID: */
     m_iWinId = (m_pMachineView && m_pMachineView->viewport()) ? (LONG64)m_pMachineView->viewport()->winId() : 0;
@@ -75,8 +78,15 @@
     /* Resize frame-buffer to default size: */
     resizeEvent(640, 480);
+
+#ifdef Q_OS_WIN
+    CoCreateFreeThreadedMarshaler(this, &m_pUnkMarshaler.p);
+#endif /* Q_OS_WIN */
+    return S_OK;
 }
 
 UIFrameBuffer::~UIFrameBuffer()
 {
+    LogRel2(("UIFrameBuffer::~UIFrameBuffer %p\n", this));
+
     /* Disconnect handlers: */
     if (m_pMachineView)
@@ -85,4 +95,14 @@
     /* Deinitialize critical-section: */
     RTCritSectDelete(&m_critSect);
+}
+
+HRESULT UIFrameBuffer::FinalConstruct()
+{
+    return 0;
+}
+
+void UIFrameBuffer::FinalRelease()
+{
+    return;
 }
 
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.h	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.h	(revision 52086)
@@ -38,5 +38,8 @@
 
 /** Common IFramebuffer implementation used to maintain VM display video memory. */
-class UIFrameBuffer : public QObject, VBOX_SCRIPTABLE_IMPL(IFramebuffer)
+class ATL_NO_VTABLE UIFrameBuffer :
+    public QObject,
+    public CComObjectRootEx<CComMultiThreadModel>,
+    VBOX_SCRIPTABLE_IMPL(IFramebuffer)
 {
     Q_OBJECT;
@@ -55,9 +58,12 @@
 public:
 
-    /** Frame-buffer constructor.
-      * @param pMachineView defines machine-view this frame-buffer is bounded to. */
-    UIFrameBuffer(UIMachineView *pMachineView);
+    /** Frame-buffer constructor. */
+    UIFrameBuffer();
     /** Frame-buffer destructor. */
     ~UIFrameBuffer();
+
+    /** Frame-buffer initialization.
+      * @param pMachineView defines machine-view this frame-buffer is bounded to. */
+    HRESULT init(UIMachineView *pMachineView);
 
     /** Assigns machine-view frame-buffer will be bounded to.
@@ -77,24 +83,16 @@
     void setAutoEnabled(bool fAutoEnabled) { m_fAutoEnabled = fAutoEnabled; }
 
-    NS_DECL_ISUPPORTS
-
-#ifdef Q_OS_WIN
-    /** Windows: Increments reference-counter. */
-    STDMETHOD_(ULONG, AddRef)()
-    {
-        return ::InterlockedIncrement(&m_iRefCnt);
-    }
-
-    /** Windows: Decrements reference-counter. */
-    STDMETHOD_(ULONG, Release)()
-    {
-        long iCounter = ::InterlockedDecrement(&m_iRefCnt);
-        if (iCounter == 0)
-            delete this;
-        return iCounter;
-    }
-#endif /* Q_OS_WIN */
-
-    VBOX_SCRIPTABLE_DISPATCH_IMPL(IFramebuffer)
+    DECLARE_NOT_AGGREGATABLE(UIFrameBuffer)
+
+    DECLARE_PROTECT_FINAL_CONSTRUCT()
+
+    BEGIN_COM_MAP(UIFrameBuffer)
+        COM_INTERFACE_ENTRY(IFramebuffer)
+        COM_INTERFACE_ENTRY2(IDispatch,IFramebuffer)
+        COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.p)
+    END_COM_MAP()
+
+    HRESULT FinalConstruct();
+    void FinalRelease();
 
     /* IFramebuffer COM methods: */
@@ -269,9 +267,4 @@
     /** RTCRITSECT object to protect frame-buffer access. */
     mutable RTCRITSECT m_critSect;
-
-#ifdef Q_OS_WIN
-    /** Windows: Holds the reference counter. */
-    long m_iRefCnt;
-#endif /* Q_OS_WIN */
 
     /** @name Scaled mode related variables.
@@ -306,4 +299,10 @@
     double m_dBackingScaleFactor;
     /** @} */
+
+private:
+
+#ifdef Q_OS_WIN
+     CComPtr <IUnknown> m_pUnkMarshaler;
+#endif /* Q_OS_WIN */
 };
 
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 52086)
@@ -379,5 +379,5 @@
         LogRelFlow(("UIMachineView::prepareFrameBuffer: Start EMT callbacks accepting for screen: %d.\n", screenId()));
         pFrameBuffer->setMarkAsUnused(false);
-        uisession()->setFrameBuffer(screenId(), pFrameBuffer);
+        m_pFrameBuffer = pFrameBuffer;
     }
     else
@@ -386,18 +386,22 @@
         if (m_fAccelerate2DVideo)
         {
-            /** these two additional template args is a workaround to
-             * this [VBox|UI] duplication
-             * @todo: they are to be removed once VBox stuff is gone */
-            pFrameBuffer = new VBoxOverlayFrameBuffer<UIFrameBuffer, UIMachineView>(this, &session(), (uint32_t)screenId());
+            ComObjPtr<VBoxOverlayFrameBuffer> pOFB;
+            pOFB.createObject();
+            pOFB->init(this, &session(), (uint32_t)screenId());
+            m_pFrameBuffer = pOFB;
         }
         else
-            pFrameBuffer = new UIFrameBuffer(this);
+        {
+            m_pFrameBuffer.createObject();
+            m_pFrameBuffer->init(this);
+        }
 # else /* VBOX_WITH_VIDEOHWACCEL */
-        pFrameBuffer = new UIFrameBuffer(this);
+        m_pFrameBuffer.createObject();
+        m_pFrameBuffer->init(this);
 # endif /* !VBOX_WITH_VIDEOHWACCEL */
-        pFrameBuffer->setHiDPIOptimizationType(uisession()->hiDPIOptimizationType());
-        uisession()->setFrameBuffer(screenId(), pFrameBuffer);
-    }
-    m_pFrameBuffer = pFrameBuffer;
+        m_pFrameBuffer->setHiDPIOptimizationType(uisession()->hiDPIOptimizationType());
+
+        uisession()->setFrameBuffer(screenId(), m_pFrameBuffer);
+    }
 
     /* If frame-buffer was prepared: */
@@ -407,12 +411,5 @@
         CDisplay display = session().GetConsole().GetDisplay();
         Assert(!display.isNull());
-        CFramebuffer fb(NULL);
-        /* Check if the framebuffer is already assigned;
-         * in this case we do not need to re-assign it neither do we need to AddRef. */
-        fb = display.QueryFramebuffer(m_uScreenId);
-        if (fb.raw() != m_pFrameBuffer) /* <-this will evaluate to true iff no framebuffer is yet assigned */
-        {
-            m_pFrameBuffer->AddRef();
-        }
+        CFramebuffer fb = display.QueryFramebuffer(m_uScreenId);
         /* Always perform AttachFramebuffer to ensure 3D gets notified: */
         if (!fb.isNull())
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h	(revision 52086)
@@ -226,5 +226,5 @@
     UIMachineWindow *m_pMachineWindow;
     ulong m_uScreenId;
-    UIFrameBuffer *m_pFrameBuffer;
+    ComObjPtr<UIFrameBuffer> m_pFrameBuffer;
     KMachineState m_previousState;
     /** HACK: when switching out of fullscreen or seamless we wish to override
@@ -260,5 +260,5 @@
     friend class UIMachineLogic;
     friend class UIFrameBuffer;
-    template<class, class> friend class VBoxOverlayFrameBuffer;
+    friend class VBoxOverlayFrameBuffer;
 };
 
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.cpp	(revision 52086)
@@ -1207,5 +1207,5 @@
             display.DetachFramebuffer(i);
             /* Release framebuffer reference: */
-            pFb->Release();
+            m_frameBufferVector[i].setNull();
         }
     }
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 52076)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UISession.h	(revision 52086)
@@ -359,5 +359,5 @@
 
     /* Frame-buffers vector: */
-    QVector<UIFrameBuffer*> m_frameBufferVector;
+    QVector<ComObjPtr<UIFrameBuffer>> m_frameBufferVector;
 
     /* Common variables: */
