Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31307)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31308)
@@ -3153,21 +3153,7 @@
     if (FAILED(rc)) return rc;
 
-    /* check that the port and device are not out of range. */
-    ULONG portCount;
-    ULONG devicesPerPort;
-    rc = ctl->COMGETTER(PortCount)(&portCount);
+    // check that the port and device are not out of range
+    rc = ctl->checkPortAndDeviceValid(aControllerPort, aDevice);
     if (FAILED(rc)) return rc;
-    rc = ctl->COMGETTER(MaxDevicesPerPortCount)(&devicesPerPort);
-    if (FAILED(rc)) return rc;
-
-    if (   (aControllerPort < 0)
-        || (aControllerPort >= (LONG)portCount)
-        || (aDevice < 0)
-        || (aDevice >= (LONG)devicesPerPort)
-       )
-        return setError(E_INVALIDARG,
-                        tr("The port and/or count parameter are out of range [%lu:%lu]"),
-                        portCount,
-                        devicesPerPort);
 
     /* check if the device slot is already busy */
@@ -3230,7 +3216,7 @@
                         medium->getLocationFull().raw());
 
-    bool indirect = false;
+    bool fImplicit = false;
     if (!medium.isNull())
-        indirect = medium->isReadOnly();
+        fImplicit = medium->isReadOnly();
     bool associate = true;
 
@@ -3246,5 +3232,5 @@
             if ((pAttachTemp = findAttachment(oldAtts, medium)))
             {
-                AssertReturn(!indirect, E_FAIL);
+                AssertReturn(!fImplicit, E_FAIL);
 
                 /* see if it's the same bus/channel/device */
@@ -3265,5 +3251,5 @@
 
         /* go further only if the attachment is to be indirect */
-        if (!indirect)
+        if (!fImplicit)
             break;
 
@@ -3333,5 +3319,5 @@
                     mediumLock.attach(medium);
                     /* not implicit, doesn't require association with this VM */
-                    indirect = false;
+                    fImplicit = false;
                     associate = false;
                     /* go right to the MediumAttachment creation */
@@ -3477,5 +3463,12 @@
     ComObjPtr<MediumAttachment> attachment;
     attachment.createObject();
-    rc = attachment->init(this, medium, aControllerName, aControllerPort, aDevice, aType, indirect, 0 /* No bandwidth limit */);
+    rc = attachment->init(this,
+                          medium,
+                          aControllerName,
+                          aControllerPort,
+                          aDevice,
+                          aType,
+                          fImplicit,
+                          0 /* No bandwidth limit */);
     if (FAILED(rc)) return rc;
 
Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 31307)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 31308)
@@ -3236,5 +3236,5 @@
     {
         /* use the default format if not */
-        AutoReadLock propsLock(m->pVirtualBox->systemProperties() COMMA_LOCKVAL_SRC_POS);
+        AutoReadLock propsLock(m->pVirtualBox->getSystemProperties() COMMA_LOCKVAL_SRC_POS);
         strFormat = m->pVirtualBox->getDefaultHardDiskFormat();
     }
@@ -4843,8 +4843,8 @@
     /* get the format object first */
     {
-        AutoReadLock propsLock(m->pVirtualBox->systemProperties() COMMA_LOCKVAL_SRC_POS);
-
-        unconst(m->formatObj)
-            = m->pVirtualBox->systemProperties()->mediumFormat(aFormat);
+        SystemProperties *pSysProps = m->pVirtualBox->getSystemProperties();
+        AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
+
+        unconst(m->formatObj) = pSysProps->mediumFormat(aFormat);
         if (m->formatObj.isNull())
             return setError(E_INVALIDARG,
Index: /trunk/src/VBox/Main/StorageControllerImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/StorageControllerImpl.cpp	(revision 31307)
+++ /trunk/src/VBox/Main/StorageControllerImpl.cpp	(revision 31308)
@@ -21,4 +21,5 @@
 #include "MachineImpl.h"
 #include "VirtualBoxImpl.h"
+#include "SystemPropertiesImpl.h"
 
 #include <iprt/string.h>
@@ -79,6 +80,11 @@
 {
     Data()
-        : pParent(NULL)
+        : pVirtualBox(NULL),
+          pSystemProperties(NULL),
+          pParent(NULL)
     { }
+
+    VirtualBox * const                  pVirtualBox;
+    SystemProperties * const            pSystemProperties;
 
     Machine * const                     pParent;
@@ -131,4 +137,7 @@
 
     m = new Data();
+
+    unconst(m->pVirtualBox) = aParent->getVirtualBox();
+    unconst(m->pSystemProperties) = m->pVirtualBox->getSystemProperties();
 
     unconst(m->pParent) = aParent;
@@ -412,16 +421,6 @@
 
     AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
-
-    ComPtr<IVirtualBox> VBox;
-    HRESULT rc = m->pParent->COMGETTER(Parent)(VBox.asOutParam());
-    if (FAILED(rc))
-        return rc;
-
-    ComPtr<ISystemProperties> sysProps;
-    rc = VBox->COMGETTER(SystemProperties)(sysProps.asOutParam());
-    if (FAILED(rc))
-        return rc;
-
-    rc = sysProps->GetMaxDevicesPerPortForStorageBus(m->bd->mStorageBus, aMaxDevices);
+    HRESULT rc = m->pSystemProperties->GetMaxDevicesPerPortForStorageBus(m->bd->mStorageBus, aMaxDevices);
+
     return rc;
 }
@@ -751,4 +750,34 @@
 }
 
+/**
+ * Returns S_OK if the given port and device numbers are within the range supported
+ * by this controller. If not, it sets an error and returns E_INVALIDARG.
+ * @param ulPort
+ * @param ulDevice
+ * @return
+ */
+HRESULT StorageController::checkPortAndDeviceValid(LONG aControllerPort,
+                                                   LONG aDevice)
+{
+    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
+
+    ULONG portCount = m->bd->mPortCount;
+    ULONG devicesPerPort;
+    HRESULT rc = m->pSystemProperties->GetMaxDevicesPerPortForStorageBus(m->bd->mStorageBus, &devicesPerPort);
+    if (FAILED(rc)) return rc;
+
+    if (   (aControllerPort < 0)
+        || (aControllerPort >= (LONG)portCount)
+        || (aDevice < 0)
+        || (aDevice >= (LONG)devicesPerPort)
+       )
+        return setError(E_INVALIDARG,
+                        tr("The port and/or count parameter are out of range [%lu:%lu]"),
+                        portCount,
+                        devicesPerPort);
+
+    return S_OK;
+}
+
 /** @note Locks objects for writing! */
 void StorageController::rollback()
Index: /trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 31307)
+++ /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 31308)
@@ -2939,5 +2939,5 @@
 }
 
-const ComObjPtr<SystemProperties>& VirtualBox::systemProperties() const
+SystemProperties* VirtualBox::getSystemProperties() const
 {
     return m->pSystemProperties;
Index: /trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MachineImpl.h	(revision 31307)
+++ /trunk/src/VBox/Main/include/MachineImpl.h	(revision 31308)
@@ -821,5 +821,5 @@
     Machine* const          mPeer;
 
-    VirtualBox* const       mParent;
+    VirtualBox * const      mParent;
 
     Shareable<Data>         mData;
Index: /trunk/src/VBox/Main/include/StorageControllerImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/StorageControllerImpl.h	(revision 31307)
+++ /trunk/src/VBox/Main/include/StorageControllerImpl.h	(revision 31308)
@@ -85,4 +85,7 @@
     ULONG getInstance() const;
 
+    HRESULT checkPortAndDeviceValid(LONG aControllerPort,
+                                    LONG aDevice);
+
     void rollback();
     void commit();
Index: /trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 31307)
+++ /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 31308)
@@ -237,5 +237,5 @@
 
     const ComObjPtr<Host>& host() const;
-    const ComObjPtr<SystemProperties>& systemProperties() const;
+    SystemProperties* getSystemProperties() const;
 #ifdef VBOX_WITH_RESOURCE_USAGE_API
     const ComObjPtr<PerformanceCollector>& performanceCollector() const;
