Skip to content

Analyzer suggestion: Prefer SafeHandle over IntPtr #42404

Closed

Description

@Mrnikbobjeff

After working on some Interop analyzers I thought about SafeHandle and how known api's could suggest using the corresponding safehandle, such as SafeFileHandle or SafeWaitHandle. This could also more broadly be applied based on the method name, e.g. methods contains alloc/free could offer a warning when an IntPtr is used instead of a custom safehandle.

Activity

  1. Dotnet-GitSync-Bot commented on Sep 17, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Sep 17, 2020
  3. added
    code-analyzer
    on Sep 17, 2020
  4. jeffschwMSFT commented on Sep 21, 2020

    @jeffschwMSFT
    Member

    @elinor-fung can you take a look?

  5. elinor-fung commented on Sep 21, 2020

    @elinor-fung
    Member

    I'd be concerned with false positives with any attempt to use the method name. For known APIs, this seems reasonable, however we have #42550, where marshalling SafeHandle doesn't allow null. We would need to address that issue before adding an analyzer like this.

    /cc @AaronRobinsonMSFT @jkoritzinsky

  6. removed
    untriaged
    on Sep 21, 2020
  7. added this to the 6.0.0 milestone on Sep 21, 2020
  8. AaronRobinsonMSFT commented on Sep 21, 2020

    @AaronRobinsonMSFT
    Member

    @elinor-fung Let's make sure we fold this into #37039.

  9. modified the milestones: 6.0.0, Future on Jun 22, 2021
  10. dotnet-policy-service commented on Dec 23, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  11. added
    backlog-cleanup-candidate
    on Dec 23, 2025
  12. dotnet-policy-service commented on Jan 6, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  13. removed this from the Future milestone on Jan 6, 2026
  14. locked and limited conversation to collaborators on Feb 5, 2026
  15. removed
    backlog-cleanup-candidate
    on Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtime.InteropServicescode-analyzer

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions