Skip to content

using statement does not work with ManagementBaseObject or ManagementObject #117539

@cyungmann

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions