Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Tutorial

RAII smart pointer wrappers for Win32 HANDLEs and Direct3D resources

ptr_arithmetic
C++ Wizard
MEMBER
прадстаўнік: 162
Дата далучэння: May 2018
Паведамленні: 73
Дзякуй: 42
1 месяцаў таму · Jul 16, 2026 8:52 PM
#1

Never leak process handles or DirectX textures again using RAII with custom deleters:

CPP
struct HandleDeleter {
    void operator()(HANDLE h) const {
        if (h && h != INVALID_HANDLE_VALUE) CloseHandle(h);
    }
};
using UniqueHandle = std::unique_ptr<void, HandleDeleter>;

// Usage:
UniqueHandle hProcess(OpenProcess(PROCESS_VM_READ, FALSE, pid));
// Handle automatically closes on function return or exception!
vtable_slayer
Senior Reverser
MEMBER
прадстаўнік: 215
Дата далучэння: Mar 2018
Паведамленні: 86
Дзякуй: 61
1 месяцаў таму · Jul 17, 2026 1:15 AM
#2

You can do the same for Direct3D COM objects using Microsoft::WRL::ComPtr<ID3D11Device>. Super clean.

segfault_sam
Member
MEMBER
прадстаўнік: 68
Дата далучэння: Mar 2019
Паведамленні: 22
Дзякуй: 57
1 месяцаў таму · Jul 17, 2026 11:24 AM
#3

RAII is why modern C++ is so robust. Zero resource leaks.