- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简化为 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只会将资源标记为删除,如果满足以下条件,资源将被有效删除:
因为在你的循环中你根本没有刷新,资源是在 GPU 上创建的,但只被标记为删除。
flush device有几种方式:
在 Direct3d11 的情况下,您最终还应该调用 ClearState(这会重置整个管道),但由于 Direct2D 通常负责取消分配内部资源,因此在您的用例中也不需要这样做。
关于c++ - 在 direct2D 中释放渐变画笔时可能会发生内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55766081/
我有一个附加了 View Controller 的 AVAudioPlayer 实例。 @property (nonatomic, retain) AVAudioPlayer *previewAudi
我是java初学者。假设我声明了一个 Account 类型的变量 Account _account = new Account("Thomas"); 然后在其他地方我做了这样的事情: _account
我在我的应用程序中使用了 3 个 UIViewController,现在我想知道当我从另一个应用程序切换到另一个 UIViewController 时释放它们是否是一个好主意。显然,这将是隐藏的,当它
我分配了一个直接缓冲区: ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); 我读过: Deallocating Direct Buf
场景。我有一个图表,我可以使用右键单击来执行平移。这非常有效。然后我完美地添加了右键菜单。 问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键菜单。 有没有办法在 Java Swing 或 Jav
我使用此代码获取 ABPerson 的姓氏 CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)personRecordRef, kABP
目前,我们在基于 C 的嵌入式应用程序中使用 malloc/free Linux 命令进行内存分配/取消分配。我听说这会导致内存碎片,因为内存分配/取消分配会导致堆大小增加/减少,从而导致性能下降。其
当我尝试释放缓冲区时遇到问题。每次我尝试将缓冲区传递给释放方法时,都会发生段错误。 Valgrind 确认段错误位于 BufferDeallocate 方法中。 ==30960== Memcheck,
我想知道何时按下或释放修改后的键(Ctrl 或 Shift)。 基本上,用户可以在按下修改键的情况下执行多次击键,而我不想在它被释放之前执行一个操作(想想 Emacs 和 Ctrl + X + S).
我编写了一个相当大的网络应用程序。它运行良好一段时间,然后慢慢开始运行缓慢,因为 DOM 节点开始爬升到 80,000 - 100,000 左右。 所以我一直在 Chrome 开发工具控制台 (DCT
我知道在像 c 这样的语言中,我需要在分配内存后释放它。 (我来自 Java),对此我有几个问题: 当我在做的时候: int array[30]; (即创建一个大小为 30 个整数的数组)与
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to release pointer from boost::shared_ptr? Detach
我有一个可以从多个后台线程访问的类,可能同时访问。我无法复制该类,因为重新创建它的内容(处理或内存方面)可能很昂贵。 也有可能在后台处理仍在继续并访问该属性时替换了此类的属性。 目前我有定期的保留/释
这个问题是对: 的扩展链接-1:Creating an image out of the ios surface and saving it Link-2:Taking Screenshots fro
我有一个实例变量 NSMutableArray* searchResults。 首先,我初始化它: self.searchResults = [[NSMutableArray alloc] init]
如果我在堆上声明一些东西,比如 char *a=new char[1000] 并且主程序停止,如果没有 delete[]<,那么分配的内存会发生什么 调用?它保留在堆上还是自动释放? 最佳答案 就C+
在开发相机应用时,我遇到了一个异常,该异常仅在我切换到其他应用时发生(onPause() 用于我的应用)。 01-15 17:22:15.017: E/AndroidRuntime(14336): F
使用 JDK 1.8 编译时出现 maven 编译器错误 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (de
将 BufferedImage 保存到磁盘(以释放内存)的最快方法是什么? 我的 Java 应用程序处理大量图像(每约 300 毫秒将图像加载到内存中)。大多数这些图像都会立即被丢弃 (gc),但每隔
使用 JDK 1.8 编译时出现 maven 编译器错误 未能在项目 DUMMY 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:
我是一名优秀的程序员,十分优秀!