Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

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.