如何解决为什么重新创建 D3D12CommandAllocator 会导致内存泄漏
我进行了以下实验
pch.h
#pragma once
#include <Unknwn.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <d3d12.h>
#include <dxgi1_4.h>
main.cpp
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
int main()
{
init_apartment();
com_ptr<ID3D12Debug> debugController;
winrt::check_hresult(D3D12GetDebugInterface(guid_of<ID3D12Debug>(),debugController.put_void()));
debugController->EnableDebugLayer();
com_ptr<IDXGIFactory4> dxgiFactory;
check_hresult(CreateDXGIFactory1(guid_of<IDXGIFactory4>(),dxgiFactory.put_void()));
com_ptr<IDXGIAdapter1> dxgiAdapter{ nullptr };
check_hresult(dxgiFactory->EnumAdapters1(0,dxgiAdapter.put()));
com_ptr<ID3D12Device> device;
check_hresult(D3D12CreateDevice(dxgiAdapter.get(),D3D_FEATURE_LEVEL_11_0,guid_of<ID3D12Device>(),device.put_void()));
com_ptr<ID3D12Device4> device4 = device.as<ID3D12Device4>();
//Block 1
//This block leads to memory leak
{
com_ptr<ID3D12GraphicsCommandList> commandList;
device4->CreateCommandList1(0,D3D12_COMMAND_LIST_TYPE_DIRECT,D3D12_COMMAND_LIST_FLAG_NONE,guid_of<ID3D12GraphicsCommandList>(),commandList.put_void());
while (true)
{
com_ptr<ID3D12CommandAllocator> commandAllocator;
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,guid_of<ID3D12CommandAllocator>(),commandAllocator.put_void());
commandList->Reset(commandAllocator.get(),nullptr);
commandList->Close();
commandAllocator->Reset();
}
}
//Block 2
//This block act normally
{
com_ptr<ID3D12GraphicsCommandList> commandList;
while (true)
{
com_ptr<ID3D12CommandAllocator> commandAllocator;
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,commandAllocator.put_void());
com_ptr<ID3D12GraphicsCommandList> commandList;
device->CreateCommandList(0,commandAllocator.get(),nullptr,commandList.put_void());
commandList->Close();
commandAllocator->Reset();
}
}
//Block 3
//This block represents most common workflow
{
com_ptr<ID3D12GraphicsCommandList> commandList;
device4->CreateCommandList1(0,commandList.put_void());
com_ptr<ID3D12CommandAllocator> commandAllocator;
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,commandAllocator.put_void());
while (true)
{
commandList->Reset(commandAllocator.get(),nullptr);
commandList->Close();
commandAllocator->Reset();
}
}
}
但是在执行 Block 2 和 Block 3 时内存使用几乎保持不变
块 1 在每一帧中重新创建 D3D12CommandAllocator。
块 2 在每一帧中重新创建 D3D12CommandAllocator 和 D3D12CommandList。
第 3 块代表最常见的工作流程。
但是为什么 Block 1 会导致内存泄漏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。