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

This is _not_ a good Dispose() call :) #2337

Open
the-exodus opened this issue Nov 15, 2019 · 1 comment
Open

This is _not_ a good Dispose() call :) #2337

the-exodus opened this issue Nov 15, 2019 · 1 comment
Assignees
Labels
C# question Further information is requested

Comments

@the-exodus
Copy link

This is not really a huge issue, but the provided example in csharp/ql/src/API Abuse/MissingDisposeCallGood.cs is actually quite bad, as it can lead to all kinds of pointer-related issues (anything from a mysterious crashes to arbitrary code execution).

As you're a security-focused code analysis service, I really think a proper implementation of the Disposable-pattern would be in order :)

class MyClass : IDisposable
{
   bool disposed = false;

   public void Dispose()
   { 
      Dispose(true);
      GC.SuppressFinalize(this);           
   }

   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 
      
      if (disposing) {
    // free any managed objects
      }
      
      // Free any unmanaged objects
      disposed = true;
   }

   ~MyClass()
   {
      Dispose(false);
   }
} 
@the-exodus the-exodus added the question Further information is requested label Nov 15, 2019
@jbj jbj added the C# label Nov 15, 2019
@calumgrant calumgrant self-assigned this Nov 18, 2019
@calumgrant
Copy link
Contributor

Hi there! Many thanks for your feedback.

Having reviewed the documentation in question, I agree that the documentation should be improved. In particular, the class should have been sealed to avoid problems of overriding the Dispose method. The reason we didn't use the dispose pattern is because it would have cluttered the sample, however it has given us a great idea for a new query, which is to find places where the dispose pattern should have been used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants