From fcb445739c116547260dae217e22f4d273d6e52d Mon Sep 17 00:00:00 2001
From: Ribhi Kamal <rbhkamal@gmail.com>
Date: Wed, 27 Mar 2013 20:06:51 -0400
Subject: [PATCH 1/1] Fixed crash when entering/leaving fullscreen

This is the same crash that happens when closing
the virtual machine window
---
 .../VirtualBox/src/runtime/UIMachineView.cpp       | 29 ++++++++++++++++++++--
 .../VirtualBox/src/runtime/UIMachineView.h         |  3 +++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp b/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
index 4b871a8..fc4a354 100644
--- a/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
+++ b/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
@@ -264,6 +264,9 @@ void UIMachineView::sltMachineStateChanged()
     m_previousState = state;
 }
 
+volatile uint32_t UIMachineView::viewLock = 0;
+volatile uint32_t UIMachineView::goingDown = 0;
+
 UIMachineView::UIMachineView(  UIMachineWindow *pMachineWindow
                              , ulong uScreenId
 #ifdef VBOX_WITH_VIDEOHWACCEL
@@ -289,10 +292,16 @@ UIMachineView::UIMachineView(  UIMachineWindow *pMachineWindow
 
     /* Prepare frame buffer: */
     prepareFrameBuffer();
+
+	InterlockedExchange(&UIMachineView::viewLock, 0);
+	InterlockedExchange(&UIMachineView::goingDown, 0);
 }
 
 UIMachineView::~UIMachineView()
 {
+	while (InterlockedExchange(&UIMachineView::viewLock, 1) == 1) {Sleep(0);} // Spin Lock
+		InterlockedExchange(&UIMachineView::goingDown, 1);
+	InterlockedExchange(&UIMachineView::viewLock, 0); // Unlock
 }
 
 void UIMachineView::prepareViewport()
@@ -699,8 +708,24 @@ void UIMachineView::setMaxGuestSize()
 
 QSize UIMachineView::maxGuestSize()
 {
-    uint64_t u64Size = ASMAtomicReadU64(&m_u64MaxGuestSize);
-    return QSize(int(RT_HI_U32(u64Size)), int(RT_LO_U32(u64Size)));
+	uint32_t w = 812;
+	uint32_t h = 634;
+
+	while (InterlockedExchange(&UIMachineView::viewLock, 1) == 1){Sleep(0);} // Spinlock
+
+	if (InterlockedExchange(&UIMachineView::goingDown, 0) == 0) {
+		uint64_t u64Size = ASMAtomicReadU64(&m_u64MaxGuestSize);
+		w = int(RT_HI_U32(u64Size));
+		h = int(RT_LO_U32(u64Size));
+	} else {
+		InterlockedExchange(&UIMachineView::goingDown, 1);
+		LogRel(("maxGuestSize: HACK ALERT! Using 812x634\n"));
+	}
+
+	// Unlock
+	InterlockedExchange(&UIMachineView::viewLock, 0);
+    
+    return QSize(w, h);
 }
 
 QSize UIMachineView::guestSizeHint()
diff --git a/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h b/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h
index 9629fc7..66491d1 100644
--- a/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h
+++ b/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h
@@ -240,6 +240,9 @@ protected:
      */
     /** @todo This should be private. */
     volatile uint64_t m_u64MaxGuestSize;
+	
+	volatile static uint32_t viewLock;
+	volatile static uint32_t goingDown;
 
 #ifdef VBOX_WITH_VIDEOHWACCEL
     bool m_fAccelerate2DVideo : 1;
-- 
1.8.0.msysgit.0

