Index: trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/DarwinKeyboard.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/DarwinKeyboard.cpp	(revision 48822)
+++ trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/DarwinKeyboard.cpp	(revision 48823)
@@ -22,5 +22,5 @@
 *******************************************************************************/
 #define LOG_GROUP LOG_GROUP_GUI
-//#define VBOX_WITH_KBD_LEDS_SYNC
+#define VBOX_WITH_KBD_LEDS_SYNC
 
 #include "DarwinKeyboard.h"
@@ -1270,4 +1270,6 @@
                 rc = kIOReturnError;
         }
+
+        CFRelease(valueRef);
     }
 
@@ -1313,6 +1315,7 @@
                 rc2 = kIOReturnError;
             }
-
-        }
+        }
+
+        CFRelease(matchingElementsArrayRef);
     }
 
@@ -1359,4 +1362,6 @@
             }
         }
+
+        CFRelease(matchingElementsArrayRef);
     }
 
@@ -1372,12 +1377,16 @@
     IOHIDElementRef pElementRef = IOHIDValueGetElement(valueRef);
 
-    if (IOHIDElementGetUsagePage(pElementRef) == kHIDPage_KeyboardOrKeypad)        /* Keyboard or keypad event */
-        if (IOHIDValueGetIntegerValue(valueRef) == 1)                              /* key has been pressed down */
-            if (IOHIDElementGetUsage(pElementRef) == kHIDUsage_KeyboardCapsLock || /* CapsLock key has been pressed */
-                IOHIDElementGetUsage(pElementRef) == kHIDUsage_KeypadNumLock)      /* ... or NumLock key has been pressed */
-            {
-                Log2(("A modifier key has been pressed\n"));
-                g_LastTouchedState = (VBoxHidLeds_t *)pData;
-            }
+    if (pElementRef)
+    {
+        if (IOHIDElementGetUsagePage(pElementRef) == kHIDPage_KeyboardOrKeypad)        /* Keyboard or keypad event */
+            if (IOHIDValueGetIntegerValue(valueRef) == 1)                              /* key has been pressed down */
+                if (IOHIDElementGetUsage(pElementRef) == kHIDUsage_KeyboardCapsLock || /* CapsLock key has been pressed */
+                    IOHIDElementGetUsage(pElementRef) == kHIDUsage_KeypadNumLock)      /* ... or NumLock key has been pressed */
+                {
+                    Log2(("A modifier key has been pressed\n"));
+                    g_LastTouchedState = (VBoxHidLeds_t *)pData;
+                }
+
+    }
 }
 
@@ -1462,4 +1471,5 @@
     CFRunLoopRemoveSource(CFRunLoopGetCurrent(), pState->pLoopSourceRef, kCFRunLoopDefaultMode);
     CFRelease(pState->pLoopSourceRef);
+    CFRelease(pState->pTapRef);
 }
 
@@ -1508,5 +1518,5 @@
     VBoxHidsState_t *hidsState;
 
-    hidsState = (VBoxHidsState_t *)RTMemAllocZ(sizeof(VBoxHidsState_t));
+    hidsState = (VBoxHidsState_t *)malloc(sizeof(VBoxHidsState_t));
     if (hidsState)
     {
@@ -1514,8 +1524,8 @@
         if (hidsState->hidManagerRef)
         {
-
             CFDictionaryRef deviceMatchingDictRef = darwinGetLedDeviceMatchingDictionary();
             if (deviceMatchingDictRef)
             {
+                IOHIDManagerScheduleWithRunLoop(hidsState->hidManagerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
                 IOHIDManagerSetDeviceMatching(hidsState->hidManagerRef, deviceMatchingDictRef);
 
@@ -1530,8 +1540,8 @@
                         /* Get all the available devices and cycle through them. */
                         hidsState->cDevices = CFSetGetCount(hidDevicesSetRef);
-                        hidsState->hidDevicesCollection = (IOHIDDeviceRef *)RTMemAllocZ((size_t)hidsState->cDevices * sizeof(IOHIDDeviceRef));
+                        hidsState->hidDevicesCollection = (IOHIDDeviceRef *)malloc((size_t)hidsState->cDevices * sizeof(IOHIDDeviceRef));
                         if (hidsState->hidDevicesCollection)
                         {
-                            hidsState->hidLedsCollection = (VBoxHidLeds_t *)RTMemAllocZ((size_t)hidsState->cDevices * sizeof(VBoxHidLeds_t));
+                            hidsState->hidLedsCollection = (VBoxHidLeds_t *)malloc((size_t)hidsState->cDevices * sizeof(VBoxHidLeds_t));
                             if (hidsState->hidLedsCollection)
                             {
@@ -1571,4 +1581,5 @@
                                     if (darwinAddCarbonGlobalKeyPressHandler(hidsState) == 0)
                                     {
+                                        CFRelease(hidDevicesSetRef);
                                         CFRelease(deviceMatchingDictRef);
                                         return hidsState;
@@ -1576,9 +1587,11 @@
                                 }
 
-                                RTMemFree(hidsState->hidLedsCollection);
+                                free(hidsState->hidLedsCollection);
                             }
 
-                            RTMemFree(hidsState->hidDevicesCollection);
+                            free(hidsState->hidDevicesCollection);
                         }
+
+                        CFRelease(hidDevicesSetRef);
                     }
 
@@ -1594,5 +1607,5 @@
         }
 
-        RTMemFree(hidsState);
+        free(hidsState);
     }
 
@@ -1630,5 +1643,6 @@
             {
                 /* Cycle through supported devices only. */
-                if (darwinHidDeviceSupported(hidsState->hidDevicesCollection[i]))
+                if (IOHIDDeviceConformsTo(hidsState->hidDevicesCollection[i], kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard)
+                 && darwinHidDeviceSupported(hidsState->hidDevicesCollection[i]))
                 {
                     rc = darwinSetDeviceLedsState(hidsState->hidDevicesCollection[i],
@@ -1643,14 +1657,16 @@
                     }
 
+                    IOHIDDeviceUnscheduleFromRunLoop(hidsState->hidDevicesCollection[i], CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
                     IOHIDDeviceRegisterInputValueCallback(hidsState->hidDevicesCollection[i], NULL, NULL);
-                    IOHIDDeviceUnscheduleFromRunLoop(hidsState->hidDevicesCollection[i], CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
                 }
             }
+
+            CFRelease(elementMatchingDict);
         }
 
         /* Free resources */
 
-        RTMemFree(hidsState->hidLedsCollection);
-        RTMemFree(hidsState->hidDevicesCollection);
+        free(hidsState->hidLedsCollection);
+        free(hidsState->hidDevicesCollection);
 
         rc = IOHIDManagerClose(hidsState->hidManagerRef, 0);
@@ -1658,11 +1674,13 @@
             Log2(("Warning! Something went wrong in attempt to close HID device manager!\n"));
 
+        IOHIDManagerUnscheduleFromRunLoop(hidsState->hidManagerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+
         CFRelease(hidsState->hidManagerRef);
 
-        RTMemFree(hidsState);
+        free(hidsState);
     }
 
     return rc2;
-#else // !VBOX_WITH_KBD_LEDS_SYNC
+#else /* !VBOX_WITH_KBD_LEDS_SYNC */
     (void)pState;
     return 0;
@@ -1693,4 +1711,5 @@
         if (deviceMatchingDictRef)
         {
+            IOHIDManagerScheduleWithRunLoop(hidManagerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
             IOHIDManagerSetDeviceMatching(hidManagerRef, deviceMatchingDictRef);
 
@@ -1705,5 +1724,5 @@
                     /* Get all the available devices and cycle through them. */
                     CFIndex cDevices = CFSetGetCount(hidDevicesSetRef);
-                    IOHIDDeviceRef *hidDevicesCollection = (IOHIDDeviceRef *)RTMemAllocZ((size_t)cDevices * sizeof(IOHIDDeviceRef));
+                    IOHIDDeviceRef *hidDevicesCollection = (IOHIDDeviceRef *)malloc((size_t)cDevices * sizeof(IOHIDDeviceRef));
                     if (hidDevicesCollection)
                     {
@@ -1731,6 +1750,8 @@
                         }
 
-                        RTMemFree(hidDevicesCollection);
+                        free(hidDevicesCollection);
                     }
+
+                    CFRelease(hidDevicesSetRef);
                 }
 
@@ -1740,4 +1761,6 @@
             }
 
+            IOHIDManagerUnscheduleFromRunLoop(hidManagerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+
             CFRelease(deviceMatchingDictRef);
         }
@@ -1745,6 +1768,5 @@
         CFRelease(hidManagerRef);
     }
-#else // !VBOX_WITH_KBD_LEDS_SYNC
-
+#else /* !VBOX_WITH_KBD_LEDS_SYNC */
     (void)fNumLockOn;
     (void)fCapsLockOn;
