Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Tutorial

RAII smart pointer wrappers for Win32 HANDLEs and Direct3D resources

ptr_arithmetic
C++ Wizard
MEMBER
Rep: 162
Join Date: May 2018
Posts: 73
Thanks: 42
1 months ago ยท 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
Rep: 215
Join Date: Mar 2018
Posts: 86
Thanks: 61
1 months ago ยท 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
Rep: 68
Join Date: Mar 2019
Posts: 22
Thanks: 57
1 months ago ยท Jul 17, 2026 11:24 AM
#3

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