DirectX 9至11 OpenSharedResource疯狂地泄漏内存.难道我做错了什么? [英] DirectX 9 to 11 OpenSharedResource is leaking memory like crazy. Am I doing something wrong?

查看:278
本文介绍了DirectX 9至11 OpenSharedResource疯狂地泄漏内存.难道我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决软件中的内存泄漏问题,其中共享内存会慢慢用尽我的应用程序的虚拟地址空间.从内存泄漏的数量来看,它很明显是纹理对象的形式.

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.

我已将错误隔离到以下代码示例.我创建了一个可共享的DX9纹理对象,从D3D11设备打开它,然后释放它.在此示例中,在Windows 8.1的NVIDIA GeForce 780 Ti上运行,我的32位进程很快用完了VAS,因为这些纹理似乎没有释放.

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.

我是否误解了API,DirectX中是否有错误,或者我的GPU驱动程序中是否有错误?任何解决此问题或与谁联系的建议,将不胜感激.

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);
}

如果我注释掉"OpenSharedResource"部分,则不会泄漏.DirectX 9纹理已创建并重复释放,没有任何问题.

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);
}

推荐答案

DirectX11中的资源发布不是即时的,它发生在引用计数为0时,而且一旦命令执行完毕也是如此.

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

因此,通常您的资源将保持活动状态,直到您调用Present或在直接的Device上下文上强制进行刷新为止.

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

如果添加:

immediatecontext11->Flush();

在您进行睡眠呼叫之前,您的DirectX11资源将被正确销毁.

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

这篇关于DirectX 9至11 OpenSharedResource疯狂地泄漏内存.难道我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆