在Direct2D中释放渐变画笔时是否可能发生内存泄漏?

时间:2019-04-19 18:18:35

标签: c++ memory-leaks directx-11 direct2d

我有一个简单的C ++程序,简化为272行代码,用于初始化Direct2D,然后执行1000次操作的循环,在此过程中,它简单地创建一个ID2D1GradientStopCollection,然后创建使用ID2D1LinearGradientBrush的内容,然后立即释放它们(发行数量降至对Release的调用为零,表示通常应将其取消分配。)

但是,在每次执行此循环后,我看到进程保留的内存增加了,并且仅在释放Direct2D工厂和设备后才释放该内存,而不是在每次调用linearGradientBrush-之后释放它。 > Release()和gradientStopCollection-> Release()就像我期望的那样。

奇怪的是,当我创建/删除SolidColorBrush而不是LinearGradientBrush(仍在创建/删除gradientStopCollection)时,再也没有观察到内存泄漏。

这是我做错的事情吗,例如由于仍然被画笔使用,导致导致gradientStopCollection仍被分配? (但是在那种情况下,我看不出为什么在两个对象上调用Release之后引用计数会降为零;而且,如果我添加AddRef或删除Release以触发on上的AddRef / Release错误计数这些对象(并在程序末尾获得非零引用计数的情况),则Direct2D调试层指示d2d1debug3.dll内部在调用堆栈上调用kernelbase.dll时出现故障,从而表明AddRef / Release计数正确;但是我仍然可以通过TaskManager观察到这种内存泄漏。)

在解决此问题方面的任何帮助将不胜感激

    #include <windows.h>
    #include <commctrl.h>

    // DirectX header files.
    #include <d2d1_1.h>
    #include <d3d11.h>
    #include <d3d11_1.h>


    bool performAllocUnallocTestLoop(ID2D1DeviceContext* in_deviceContext) {
        ID2D1GradientStopCollection* gradientStopCollection;
        int i;
        ID2D1LinearGradientBrush* linearGradientBrush;
        int cnt;
        ULONG nb;
        D2D1_GRADIENT_STOP gradientStops[2];
        HRESULT hr;
        ID2D1SolidColorBrush* solidColorBrush;

        gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Yellow, 1);
        gradientStops[0].position = 0.0f;
        gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::ForestGreen, 1);
        gradientStops[1].position = 1.0f;

        cnt = 1000;
        for (i = 0; i < cnt; i++) {
            hr = in_deviceContext->CreateGradientStopCollection(gradientStops, 2, D2D1_GAMMA_2_2, D2D1_EXTEND_MODE_CLAMP, &gradientStopCollection);
            if (!SUCCEEDED(hr)) {
                return false;
            }

            hr = in_deviceContext->CreateLinearGradientBrush(D2D1::LinearGradientBrushProperties(D2D1::Point2F(0, 0), D2D1::Point2F(150, 150)), gradientStopCollection, &linearGradientBrush);
            if (!SUCCEEDED(hr)) {
                gradientStopCollection->Release();
                return false;
            }

            /*
                // This code is commented and creates a solidColorBrush instead of a linearGradientBrush.
                // Uncomment this code and comment the creation of the linearGradientBrush above instead and the leak disappears.

            hr = in_deviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Yellow, 1), &solidColorBrush);
            if (!SUCCEEDED(hr)) {
                gradientStopCollection->Release();
                return false;
            }
            */

            nb = linearGradientBrush->Release(); // Comment this line and the linearGradientBrush creation, then uncomment the solidColorBrush creation/release instead
                                 // and the memory leak disappears.
            // nb = solidColorBrush->Release();
            nb = gradientStopCollection->Release();
        }

        return true;
    }

    int main_test_function(ID2D1DeviceContext* in_deviceContext) {
        int i;

        MessageBoxW(NULL, (WCHAR*)L"Before", (WCHAR*)L"Before", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND);

        for (i = 0; i < 10; i++) {
            if (!performAllocUnallocTestLoop(in_deviceContext)) {
                MessageBoxW(NULL, (WCHAR*)L"Some creation failed", (WCHAR*)L"Some creation failed", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND);
                return 0;
            }

            if (MessageBoxW(NULL, (WCHAR*)L"After performed another 1000 create/delete of gradientStopCollection+linearGradientBrush (leak observed in TaskManager, resource NOT released)", (WCHAR*)L"After", MB_OKCANCEL | MB_ICONINFORMATION | MB_SETFOREGROUND) == IDCANCEL) {
                break;
            }
        }

        return 0;
    }

    // _____________________________________________________________
    // _ WinMain part: init/dest D3D and D2D factories and devices.
    // _____________________________________________________________

    struct TD2DFactoriesAndDevices {
        ID2D1Factory1* d2dFactory;

        ID3D11Device1* d3dDevice;
        IDXGIDevice* dxgiDevice;
        ID2D1Device* d2dDevice;
        ID2D1DeviceContext* d2dDeviceContext;
    };

    void destFactoriesAndDevices(TD2DFactoriesAndDevices* in_struct) {

        if (in_struct == NULL) {
            return;
        }

        if (in_struct->d3dDevice != NULL) {
            in_struct->d3dDevice->Release();
            in_struct->d3dDevice = NULL;
        }
        if (in_struct->dxgiDevice != NULL) {
            in_struct->dxgiDevice->Release();
            in_struct->dxgiDevice = NULL;
        }
        if (in_struct->d2dDevice != NULL) {
            in_struct->d2dDevice->Release();
            in_struct->d2dDevice = NULL;
        }
        if (in_struct->d2dDeviceContext != NULL) {
            in_struct->d2dDeviceContext->Release();
            in_struct->d2dDeviceContext = NULL;
        }

        if (in_struct->d2dFactory != NULL) {
            in_struct->d2dFactory->Release();
            in_struct->d2dFactory = NULL;
        }

        CoUninitialize();
    }

    bool initFactoriesAndDevices(TD2DFactoriesAndDevices* in_struct,
                                     const wchar_t*& out_error) {
        HRESULT hr;
        D2D1_FACTORY_OPTIONS options;
        ID3D11Device* d3dDevice;
        UINT createDeviceFlags;
        D3D_DRIVER_TYPE driverType;

        if (in_struct == NULL) {
            out_error = L"Can not use NULL pointer";
            return false;
        }

        in_struct->d3dDevice = NULL;
        in_struct->dxgiDevice = NULL;
        in_struct->d2dDevice = NULL;
        in_struct->d2dDeviceContext = NULL;

        in_struct->d2dFactory = NULL;

        // init DCOM
        if (CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) != S_OK) {
            out_error = L"Could not init DCOM";
            return false;
        }

        // Create the Direct2D factory.
        ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
    #if defined(_DEBUG)

        options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
        // options.debugLevel = D2D1_DEBUG_LEVEL_NONE;
    #endif

        hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
                                       options, &in_struct->d2dFactory);
        if (!SUCCEEDED(hr)) {
            in_struct->d2dFactory = NULL;
            out_error = L"D2D1CreateFactory failed";
            CoUninitialize();
            return false;
        }

    __create_devices:

        // Create the D3D device on default adapter.
        createDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
        createDeviceFlags |= D3D11_CREATE_DEVICE_SINGLETHREADED;
    #ifdef _DEBUG
        createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
    #endif

        // Creating the d3dDevice
        in_struct->d3dDevice = NULL;

        D3D_FEATURE_LEVEL featureLevels[] =
        {
            D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL_10_0,
            D3D_FEATURE_LEVEL_9_3,
            D3D_FEATURE_LEVEL_9_2,
            D3D_FEATURE_LEVEL_9_1
        };


        hr = D3D11CreateDevice(
            nullptr,
            D3D_DRIVER_TYPE_HARDWARE,
            0,
            createDeviceFlags,
            featureLevels,
            ARRAYSIZE(featureLevels),
            D3D11_SDK_VERSION,
            &d3dDevice,
            nullptr,
            nullptr
            );



        if (FAILED(hr)) {
            in_struct->d3dDevice = NULL;
            goto __create_failed;
        }

        hr = d3dDevice->QueryInterface(__uuidof(ID3D11Device1),
                                              (void**)(&in_struct->d3dDevice));
        if (FAILED(hr)) {
            in_struct->d3dDevice = NULL;
            goto __create_failed;
        }

        d3dDevice->Release();

        // Get a DXGI device interface from the D3D device.
        in_struct->dxgiDevice = NULL;
        hr = in_struct->d3dDevice->QueryInterface(__uuidof(IDXGIDevice),
                                           (void**)(&in_struct->dxgiDevice));
        if (FAILED(hr)) {
            in_struct->dxgiDevice = NULL;
            goto __create_failed;
        }

        // Create a D2D device from the DXGI device.
        hr = in_struct->d2dFactory->CreateDevice(in_struct->dxgiDevice,
                                                         &in_struct->d2dDevice);
        if (FAILED(hr)) {
            in_struct->d2dDevice = NULL;
            goto __create_failed;
        }

        // Create a device context from the D2D device, to create the
                // resources.
        hr = in_struct->d2dDevice->CreateDeviceContext(
                D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &in_struct->d2dDeviceContext);
        if (FAILED(hr)) {
            in_struct->d2dDeviceContext = NULL;
            goto __create_failed;
        }

        // Setting isCreated
    __after_create_devices:

        return true;


    __create_failed:
        destFactoriesAndDevices(in_struct);

        out_error = L"Could not create the devices";
        return false;
    }

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                             LPSTR lpCmdLine, int nCmdShow) {
        int result;
        const wchar_t* _error;
        TD2DFactoriesAndDevices _struct;

        if (!initFactoriesAndDevices(&_struct, _error)) {
            MessageBoxW(NULL, (WCHAR*)L"An error occured", _error,
                                           MB_OK | MB_ICONINFORMATION);
            return 0;
        }

        result = main_test_function(_struct.d2dDeviceContext);

        destFactoriesAndDevices(&_struct);

        return result;
    }

这已经在两台不同的机器上进行了测试,并且泄漏出现在x86 / x64调试/发布平台上

1 个答案:

答案 0 :(得分:0)

考虑您的代码,这是预期的行为。

在内部,Direct3D11会进行一些资源跟踪,并且调用release只会将资源标记为删除,如果满足以下条件,则资源将被有效删除:

  • DeviceContext崩溃
  • 资源外部计数器为0(Release返回的计数器)
  • 资源内部计数器为0,当您将资源连接到管道(从您的情况下,由Direct2D处理)时,资源内部计数器就会增加

由于在循环中根本不刷新,因此资源是在GPU上创建的,但仅标记为删除。

有几种冲洗设备的方法:

  • 调用EndDraw(或出现在交换链上)时,将其显示在屏幕上。
  • 当您在Direct3D设备上下文上手动执行“刷新”时(请注意,如果您实际在屏幕上执行渲染,则不应手动调用“刷新”,因为Present / EndDraw会处理这一问题)
  • 当您为cpu回读映射资源时(我认为用例超出范围)
  • 内部命令缓冲区已满时(因为在这种情况下,如果永远不填充,就不会执行任何图形调用)。

在Direct3d11情况下,您最终还应该调用ClearState(这将重置整个管道),但是由于Direct2D通常负责取消分配内部资源,因此在您的用例中也不需要。