gpt4 book ai didi

c++ - 在 direct2D 中释放渐变画笔时可能会发生内存泄漏?

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:26 25 4
gpt4 key购买 nike

我有一个简化为 272 行代码的简单 C++ 程序,它初始化 Direct2D,然后执行 1000 次操作的循环,它只创建一个 ID2D1GradientStopCollection,然后创建使用 ID2D1LinearGradientBrush,然后立即释放它们(释放计数下降到对 Release 的调用为零,因此表明它们通常应该未分配)。

然而,每次执行这个循环后,我看到进程保留的内存增加,并且只在最后释放,一旦 direct2D 工厂和设备被释放,而不是在每次调用 linearGradientBrush 后被释放->Release() 和 gradientStopCollection->Release() 如我所料。

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

这是我做错了什么,导致例如 gradientStopCollection 仍然被分配,因为仍然被画笔使用? (但在那种情况下,我不明白为什么在两个对象上调用 Release 后引用计数降为零;而且,如果我添加一个 AddRef 或删除一个 Release 以触发错误的 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、调试/发布平台上

最佳答案

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

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

  • DeviceContext 已刷新
  • 资源外部计数器为0(Release返回的那个)
  • 资源内部计数器为 0,当您的资源被附加、从管道分离(在您的情况下是 Direct2D 处理它)时,它会递增

因为在你的循环中你根本没有刷新,资源是在 GPU 上创建的,但只被标记为删除。

flush device有几种方式:

  • 当您调用 EndDraw(或出现在 Swapchain 上)时,在屏幕上显示。
  • 当您在 Direct3D Device Context 上手动执行 Flush 时(请注意,如果您实际在屏幕上执行渲染,则不应手动调用 Flush,因为 Present/EndDraw 会处理)
  • 当您为 cpu 回读映射资源时(我猜超出了您的用例范围)
  • 当内部命令缓冲区已满时(因为在您的情况下您不会执行任何绘图调用,如果永远不会被填满的话)。

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

关于c++ - 在 direct2D 中释放渐变画笔时可能会发生内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55766081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com