Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Dispose unmanaged objects automatically using IDisposable #16071

Closed
0x5bfa opened this issue Aug 25, 2024 · 1 comment · Fixed by #16152
Closed

Code Quality: Dispose unmanaged objects automatically using IDisposable #16071

0x5bfa opened this issue Aug 25, 2024 · 1 comment · Fixed by #16152
Assignees

Comments

@0x5bfa
Copy link
Member

0x5bfa commented Aug 25, 2024

Description

I'd propose disposable structs in order to avoid memory leaks due to undisposed unmanaged objects.

The structs

  • ComPtr
  • ComHeapPtr

Usage

{
    try
    {
        using ComPtr<T1> pObj1 = default;
        var hr = CoCreateInstance(..., (void**)pObj1.GetAddressOf());
        var hr = pObj1.Get()->Method1(...);

        using ComHeapPtr<LPWSTR> pDisplayName = default;
    }
    catch (Exception ex)
    {
        // Log the exception
    }
}

Concerned code

  • All com related unmanaged objects.

Gains

  • prevent memory leaks

Requirements

  • Implement the structs that implements IDisposable and use it through 'using' statement

Comments

No response

@0x5bfa 0x5bfa self-assigned this Aug 25, 2024
@0x5bfa

This comment was marked as outdated.

@0x5bfa 0x5bfa changed the title Code Quality: Dispose COM pointers automatically using IDisposable Code Quality: Dispose unmanaged objects automatically using IDisposable Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
1 participant