Index: /trunk/src/VBox/Frontends/VBoxSDL/VBoxSDL.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VBoxSDL/VBoxSDL.cpp	(revision 55132)
+++ /trunk/src/VBox/Frontends/VBoxSDL/VBoxSDL.cpp	(revision 55133)
@@ -208,4 +208,5 @@
 static ULONG       gcMonitors = 1;
 static ComObjPtr<VBoxSDLFB> gpFramebuffer[64];
+static Bstr gaFramebufferId[64];
 static SDL_Cursor *gpDefaultCursor = NULL;
 #ifdef VBOXSDL_WITH_X11
@@ -2008,5 +2009,5 @@
     {
         // register our framebuffer
-        rc = gpDisplay->AttachFramebuffer(i, gpFramebuffer[i]);
+        rc = gpDisplay->AttachFramebuffer(i, gpFramebuffer[i], gaFramebufferId[i].asOutParam());
         if (FAILED(rc))
         {
@@ -3028,5 +3029,5 @@
     {
         for (unsigned i = 0; i < gcMonitors; i++)
-            gpDisplay->DetachFramebuffer(i);
+            gpDisplay->DetachFramebuffer(i, gaFramebufferId[i].raw());
     }
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp	(revision 55132)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIFrameBuffer.cpp	(revision 55133)
@@ -381,4 +381,6 @@
      CComPtr <IUnknown> m_pUnkMarshaler;
 #endif /* Q_OS_WIN */
+     /** Identifier returned by AttachFramebuffer. Used in DetachFramebuffer. */
+     QString m_strFramebufferId;
 };
 
@@ -599,5 +601,5 @@
 void UIFrameBufferPrivate::attach()
 {
-    display().AttachFramebuffer(m_uScreenId, CFramebuffer(this));
+    m_strFramebufferId = display().AttachFramebuffer(m_uScreenId, CFramebuffer(this));
 }
 
@@ -606,5 +608,8 @@
     CFramebuffer frameBuffer = display().QueryFramebuffer(m_uScreenId);
     if (!frameBuffer.isNull())
-        display().DetachFramebuffer(m_uScreenId);
+    {
+        display().DetachFramebuffer(m_uScreenId, m_strFramebufferId);
+        m_strFramebufferId.clear();
+    }
 }
 
Index: /trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 55132)
+++ /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 55133)
@@ -15938,5 +15938,5 @@
   <interface
     name="IDisplay" extends="$unknown"
-    uuid="04AA5E91-3816-4990-8C52-59939307E5EF"
+    uuid="94a7faa2-7792-42a3-8535-00770eca1f53"
     wsmap="managed"
     wrap-hint-server-addinterfaces="IEventListener"
@@ -15978,4 +15978,5 @@
       <param name="screenId" type="unsigned long" dir="in"/>
       <param name="framebuffer" type="IFramebuffer" dir="in"/>
+      <param name="id" type="uuid" mod="string" dir="return"/>
     </method>
 
@@ -15985,4 +15986,5 @@
       </desc>
       <param name="screenId" type="unsigned long" dir="in"/>
+      <param name="id" type="uuid" mod="string" dir="in"/>
     </method>
 
Index: /trunk/src/VBox/Main/include/DisplayImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/DisplayImpl.h	(revision 55132)
+++ /trunk/src/VBox/Main/include/DisplayImpl.h	(revision 55133)
@@ -48,4 +48,5 @@
 
     ComPtr<IFramebuffer> pFramebuffer;
+    com::Guid framebufferId;
     ComPtr<IDisplaySourceBitmap> pSourceBitmap;
     bool fDisabled;
@@ -225,6 +226,8 @@
                                         GuestMonitorStatus_T *aGuestMonitorStatus);
     virtual HRESULT attachFramebuffer(ULONG aScreenId,
-                                      const ComPtr<IFramebuffer> &aFramebuffer);
-    virtual HRESULT detachFramebuffer(ULONG aScreenId);
+                                      const ComPtr<IFramebuffer> &aFramebuffer,
+                                      com::Guid &aId);
+    virtual HRESULT detachFramebuffer(ULONG aScreenId,
+                                      const com::Guid &aId);
     virtual HRESULT queryFramebuffer(ULONG aScreenId,
                                      ComPtr<IFramebuffer> &aFramebuffer);
Index: /trunk/src/VBox/Main/src-client/DisplayImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-client/DisplayImpl.cpp	(revision 55132)
+++ /trunk/src/VBox/Main/src-client/DisplayImpl.cpp	(revision 55133)
@@ -1602,5 +1602,5 @@
 
 
-HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer)
+HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
 {
     LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
@@ -1618,4 +1618,6 @@
 
     pFBInfo->pFramebuffer = aFramebuffer;
+    pFBInfo->framebufferId.create();
+    aId = pFBInfo->framebufferId;
 
     SafeArray<FramebufferCapabilities_T> caps;
@@ -1663,11 +1665,11 @@
     }
 
-    LogRelFlowFunc(("Attached to %d\n", aScreenId));
+    LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
     return S_OK;
 }
 
-HRESULT Display::detachFramebuffer(ULONG aScreenId)
-{
-    LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
+HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
+{
+    LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
 
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
@@ -1679,5 +1681,12 @@
     DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
 
+    if (pFBInfo->framebufferId != aId)
+    {
+        LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
+        return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
+    }
+
     pFBInfo->pFramebuffer.setNull();
+    pFBInfo->framebufferId.clear();
 
     alock.release();
