Skip to content

Commit

Permalink
Fixed a memory leak caused by unreleased COM objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRiches committed Sep 20, 2016
1 parent 8051205 commit 88a9dfb
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 157 deletions.
80 changes: 48 additions & 32 deletions src/libANGLE/renderer/d3d/d3d11/winrt/HolographicNativeWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ bool HolographicNativeWindow::initialize(EGLNativeWindowType holographicSpace, I
{
mInitialized = true;

ComPtr<IPropertySet> props = propertySet;
ComPtr<IInspectable> space = holographicSpace;
ComPtr<IPropertySet> spProps = propertySet;
ComPtr<IInspectable> spSpace = holographicSpace;
SIZE swapChainSize = {};
HRESULT result = S_OK;

if (SUCCEEDED(result))
{
result = space.As(&mHolographicSpace);
result = spSpace.As(&mHolographicSpace);
}

ComPtr<ABI::Windows::Foundation::Collections::IMap<HSTRING, IInspectable*>> propertyMap;
ComPtr<ABI::Windows::Foundation::Collections::IMap<HSTRING, IInspectable*>> spPropertyMap;
if (SUCCEEDED(result))
{
result = props.As(&propertyMap);
result = spProps.As(&spPropertyMap);
}

{
// Look for the presence of the EGLNativeWindowType in the property set.
boolean hasEglBaseCoordinateSystemProperty = false;
if (SUCCEEDED(result))
{
result = propertyMap->HasKey(HStringReference(EGLBaseCoordinateSystemProperty).Get(), &hasEglBaseCoordinateSystemProperty);
result = spPropertyMap->HasKey(HStringReference(EGLBaseCoordinateSystemProperty).Get(), &hasEglBaseCoordinateSystemProperty);
}

// If the IPropertySet does not contain the required EGLBaseCoordinateSystemProperty key, the property set is
Expand All @@ -89,7 +89,7 @@ bool HolographicNativeWindow::initialize(EGLNativeWindowType holographicSpace, I
ComPtr<IInspectable> frameOfReference;
if (SUCCEEDED(result) && hasEglBaseCoordinateSystemProperty)
{
result = propertyMap->Lookup(HStringReference(EGLBaseCoordinateSystemProperty).Get(), &frameOfReference);
result = spPropertyMap->Lookup(HStringReference(EGLBaseCoordinateSystemProperty).Get(), &frameOfReference);
}

if (SetSpatialFrameOfReference(frameOfReference.Get()).getCode() == EGL_BAD_PARAMETER)
Expand All @@ -105,14 +105,14 @@ bool HolographicNativeWindow::initialize(EGLNativeWindowType holographicSpace, I
boolean hasEglAutomaticStereoRenderingProperty = false;
if (SUCCEEDED(result))
{
result = propertyMap->HasKey(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &hasEglAutomaticStereoRenderingProperty);
result = spPropertyMap->HasKey(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &hasEglAutomaticStereoRenderingProperty);
}

// The property exists, so retrieve the IInspectable that represents it.
ComPtr<IInspectable> enableAutomaticStereoRenderingPropertyInspectable;
if (SUCCEEDED(result) && hasEglAutomaticStereoRenderingProperty)
{
result = propertyMap->Lookup(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &enableAutomaticStereoRenderingPropertyInspectable);
result = spPropertyMap->Lookup(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &enableAutomaticStereoRenderingPropertyInspectable);
}

ComPtr<IPropertyValue> enableAutomaticStereoRenderingProperty;
Expand All @@ -137,14 +137,14 @@ bool HolographicNativeWindow::initialize(EGLNativeWindowType holographicSpace, I
boolean hasEglAutomaticDepthBasedImageStabilizationProperty = false;
if (SUCCEEDED(result))
{
result = propertyMap->HasKey(HStringReference(EGLAutomaticDepthBasedImageStabilizationProperty).Get(), &hasEglAutomaticDepthBasedImageStabilizationProperty);
result = spPropertyMap->HasKey(HStringReference(EGLAutomaticDepthBasedImageStabilizationProperty).Get(), &hasEglAutomaticDepthBasedImageStabilizationProperty);
}

// The property exists, so retrieve the IInspectable that represents it.
ComPtr<IInspectable> enableAutomaticDepthBasedImageStabilizationPropertyInspectable;
if (SUCCEEDED(result) && hasEglAutomaticDepthBasedImageStabilizationProperty)
{
result = propertyMap->Lookup(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &enableAutomaticDepthBasedImageStabilizationPropertyInspectable);
result = spPropertyMap->Lookup(HStringReference(EGLAutomaticStereoRenderingProperty).Get(), &enableAutomaticDepthBasedImageStabilizationPropertyInspectable);
}

ComPtr<IPropertyValue> enableAutomaticDepthBasedImageStabilizationProperty;
Expand Down Expand Up @@ -173,18 +173,18 @@ bool HolographicNativeWindow::initialize(EGLNativeWindowType holographicSpace, I

bool HolographicNativeWindow::registerForHolographicCameraEvents()
{
ComPtr<IHolographicSpaceCameraAddedEventArgs> cameraAddedHandler;
HRESULT result = Microsoft::WRL::MakeAndInitialize<CameraAddedEventHandler>(cameraAddedHandler.ReleaseAndGetAddressOf(), this->shared_from_this());
ComPtr<IHolographicSpaceCameraAddedEventArgs> spCameraAddedHandler;
HRESULT result = Microsoft::WRL::MakeAndInitialize<CameraAddedEventHandler>(spCameraAddedHandler.ReleaseAndGetAddressOf(), this->shared_from_this());
if (SUCCEEDED(result))
{
result = mHolographicSpace->add_CameraAdded(cameraAddedHandler.Get(), &mCameraAddedEventToken);
result = mHolographicSpace->add_CameraAdded(spCameraAddedHandler.Get(), &mCameraAddedEventToken);
}

ComPtr<IHolographicSpaceCameraRemovedEventArgs> cameraRemovedHandler;
result = Microsoft::WRL::MakeAndInitialize<CameraRemovedEventHandler>(cameraRemovedHandler.ReleaseAndGetAddressOf(), this->shared_from_this());
ComPtr<IHolographicSpaceCameraRemovedEventArgs> spCameraRemovedHandler;
result = Microsoft::WRL::MakeAndInitialize<CameraRemovedEventHandler>(spCameraRemovedHandler.ReleaseAndGetAddressOf(), this->shared_from_this());
if (SUCCEEDED(result))
{
result = mHolographicSpace->add_CameraRemoved(cameraRemovedHandler.Get(), &mCameraRemovedEventToken);
result = mHolographicSpace->add_CameraRemoved(spCameraRemovedHandler.Get(), &mCameraRemovedEventToken);
}

if (SUCCEEDED(result))
Expand Down Expand Up @@ -254,8 +254,8 @@ HRESULT HolographicNativeWindow::createPreferredD3DDevice(ID3D11Device *givenDev
};

// Create the Direct3D 11 API device object and a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<ID3D11Device> spNewDevice;
ComPtr<ID3D11DeviceContext> spNewContext;
HRESULT hr = D3D11CreateDevice(
mPreferredDxgiAdapter.Get(),// The primary DXGI adapter determined to support a Windows Holographic device.
D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver.
Expand All @@ -264,29 +264,35 @@ HRESULT HolographicNativeWindow::createPreferredD3DDevice(ID3D11Device *givenDev
featureLevels, // List of feature levels this app can support.
ARRAYSIZE(featureLevels), // Size of the list above.
D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
&device, // Returns the Direct3D device created.
&spNewDevice, // Returns the Direct3D device created.
nullptr,
&context // Returns the device immediate context.
&spNewContext // Returns the device immediate context.
);

if (FAILED(hr))
// Store pointers to the Direct3D device and immediate context.
if (SUCCEEDED(hr))
{
// If the initialization fails, fall back to whatever D3D device we were given.
ComPtr<ID3D11Device> spDevice = givenDevice;
hr = device.As(&mPreferredD3DDevice);
mPreferredDxgiDevice.Reset();
mPreferredDxgiAdapter.Reset();
mPreferredD3DContext.Reset();
hr = spNewDevice.As(&mPreferredD3DDevice);
}

// Store pointers to the Direct3D device and immediate context.
if (SUCCEEDED(hr))
{
hr = device.As(&mPreferredD3DDevice);
hr = spNewContext.As(&mPreferredD3DContext);
}
if (SUCCEEDED(hr))

if (FAILED(hr))
{
hr = context.As(&mPreferredD3DContext);
// If the initialization fails, fall back to whatever D3D device we were given.
ComPtr<ID3D11Device> spDevice = givenDevice;
hr = spDevice.As(&mPreferredD3DDevice);

if (SUCCEEDED(hr))
{
mPreferredD3DDevice->GetImmediateContext3(mPreferredD3DContext.ReleaseAndGetAddressOf());
}

mPreferredDxgiDevice.Reset();
mPreferredDxgiAdapter.Reset();
}

// Acquire the DXGI interface for the Direct3D device.
Expand Down Expand Up @@ -727,4 +733,14 @@ HRESULT HolographicNativeWindow::GetHolographicCameraPose(UINT32 id, ABI::Window
return mHolographicCameraPoses->GetAt(id, outPose);
};

void HolographicNativeWindow::ResetFrame()
{
// Reset the frame update state, triggering a holographic resource update for
// the next frame.
mUpdateNeeded = true;
mHolographicFrame.Reset();
mCoordinateSystem.Reset();
mHolographicCameraPoses.Reset();
}

}
10 changes: 1 addition & 9 deletions src/libANGLE/renderer/d3d/d3d11/winrt/HolographicNativeWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,7 @@ class HolographicNativeWindow : public InspectableNativeWindow, public std::enab
return holographicCameraListChangedReturnValue;
}
bool IsUpdateNeeded() { return mUpdateNeeded; }
void ResetFrame()
{
// Reset the frame update state, triggering a holographic resource update for
// the next frame.
mUpdateNeeded = true;
mHolographicFrame = nullptr;
mCoordinateSystem = nullptr;
mHolographicCameraPoses.Reset();
}
void ResetFrame();

protected:
HRESULT initializeHolographicSpaceUsingDirect3DDevice(ID3D11Device *device);
Expand Down
Loading

1 comment on commit 88a9dfb

@mlfarrell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged this in and it fixed all of my memory leaks thanks a million!

I had trouble sifting through the renames -> sp prefix vs the actual memory leak causes. What was the actual culprit for most of the leaks?

Please sign in to comment.