Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3222. Potential resource leak. An...
menu mobile close menu
Additional information
toggle menu Contents

V3222. Potential resource leak. An inner IDisposable object might remain non-disposed if the constructor of the outer object throws an exception.

May 27 2025

The analyzer has detected a potential resource leak. If creating an object causes an exception, the inner object already created may not be properly released.

The example:

public void EditFile(string f)
{
  using (BufferedStream os = new BufferedStream(new FileStream(f, 
                                                FileMode.Open)))
  {
    ....
  }
}

In this case, an object of the FileStream class may be created successfully, but an exception is thrown when creating an object of the BufferedStrem class. As a result, the Dispose method is not be called, and the file descriptor is not released.

To secure the code, consider the following approach:

public void EditFile(string f)
{
  using (FileStream fileStream = new FileStream(f, FileMode.Open))
  {
    using (BufferedStream os = new BufferedStream(fileStream))
    {
      ....
    }
  }
  
}

In this implementation, the using statement creates an object of the FileStream class in advance, ensuring it is properly released and preventing the resource leak.

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam