DirectX 9 to 11 OpenSharedResource is leaking memory like crazy. Am I doing something wrong?

北城以北 提交于 2021-01-29 06:17:53

问题


I've been fighting a memory leak in my software, where the virtual address space of my application is slowly used up by shared memory. Based on the amount of memory leaked, it was very clearly in the form of texture objects.

I've isolated the bug to the following code sample. I created a share-able DX9 texture object, I open it from a D3D11 device, and then I release it. In this sample, running on my NVIDIA GeForce 780 Ti on Windows 8.1, my 32-bit process very quickly runs out of VAS as these textures do not appear to get freed.

Am I misunderstanding an API, is there a bug in DirectX, or is there a bug in my GPU driver? Any suggestions on fixing this, or who to contact, are greatly appreciated.

while (true)
{
    IDirect3DTexture9* tex = nullptr;
    HANDLE handle = 0;
    hr = device9->CreateTexture(1024, 1024, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &tex, &handle);
    VERIFY_SUCCEEDED(hr);

    ID3D11Texture2D* tex11;
    hr = device11->OpenSharedResource(handle, __uuidof(ID3D11Texture2D), (void**)&tex11);
    VERIFY_SUCCEEDED(hr);

    tex11->Release();
    tex->Release();

    Sleep(10);
}

If I comment out the "OpenSharedResource" section there is no leak. The DirectX 9 textures are created and freed repeatedly with no issue.

while (true)
{
    IDirect3DTexture9* tex = nullptr;
    HANDLE handle = 0;
    hr = device9->CreateTexture(1024, 1024, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &tex, &handle);
    VERIFY_SUCCEEDED(hr);

    //ID3D11Texture2D* tex11;
    //hr = device11->OpenSharedResource(handle, __uuidof(ID3D11Texture2D), (void**)&tex11);
    //VERIFY_SUCCEEDED(hr);

    //tex11->Release();
    tex->Release();

    Sleep(10);
}

回答1:


Resource release in DirectX11 is not immediate, it happens when reference count is 0, but also once commands have finished execution.

So generally your resource will stay alive either until you call Present or force a Flush on the immediate Device context.

If you add:

immediatecontext11->Flush();

Before your sleep call, your DirectX11 resource will be destroyed properly.



来源:https://stackoverflow.com/questions/32428682/directx-9-to-11-opensharedresource-is-leaking-memory-like-crazy-am-i-doing-some

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!