Skip to content

using statement does not work with ManagementBaseObject or ManagementObject #117539

Open

Description

@cyungmann

Description

Using statements do not properly dispose of System.Management.ManagementBaseObject or System.Management.ManagementObject due to both classes defining new Dispose() methods that hide the base class method with new rather than overriding the virtual void Dispose(bool disposing) from System.ComponentModel.Component.

Reproduction Steps

using System.Management;

using (var mo = new ManagementObject()) {

} // step through with a debugger and observe that neither ManagementObject.Dispose() nor ManagementBaseObject.Dispose() are called, but only Componnet.Dispose()

Expected behavior

ManagementObject and ManagementBaseObject have disposal code called.

Actual behavior

Only Component has its disposal code called.

Regression?

No

Known Workarounds

Avoid using using statements with these classes and instead use a try/finally.

Configuration

.NET 9
Windows 10 Enterprise
x64

Other information

See also https://stackoverflow.com/a/11896367/9287029

Activity

  1. added
    untriaged
    on Jul 11, 2025
  2. dotnet-policy-service commented on Jul 11, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-management
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Jul 14, 2025
  4. added this to the Future milestone on Jul 14, 2025
  5. ericstj commented on Jul 14, 2025

    @ericstj
    Member

    Not clear why this is the case, but it's been that way since .NET 2.0. I couldn't find anything in the internal source history to indicate why. System.Management is in maintenance mode so we're unlikely to change this.

  6. cyungmann commented on Jul 14, 2025

    @cyungmann
    Author

    @ericstj is there a replacement for performing WMI queries from C# code?

  7. ericstj commented on Jul 14, 2025

    @ericstj
    Member

    At one point that was supposed to be Microsoft Management Infrastructure - but not sure any more. This portion of Windows is owned by the Powershell folks - you can ask that question over in https://github.com/powershell/powershell. They may reccomend that folks use platform API rather than WMI.

  8. cyungmann commented on Jul 17, 2025

    @cyungmann
    Author

    Thank you, @ericstj. There is this discussion in the PowerShell repo open since January with no response. Any suggestions for bringing attention to it / getting a response?

  9. loudenvier commented on Sep 22, 2025

    @loudenvier

    Thank you, @ericstj. There is this discussion in the PowerShell repo open since January with no response. Any suggestions for bringing attention to it / getting a response?

    To add insult to injury the repo is archived... so I really can't know what library to use...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions