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

Resource Leak Checker: calling @CreatesMustCallFor("this") method from constructor #5175

Closed
mernst opened this issue Jun 24, 2022 · 0 comments · Fixed by #5179
Closed

Resource Leak Checker: calling @CreatesMustCallFor("this") method from constructor #5175

mernst opened this issue Jun 24, 2022 · 0 comments · Fixed by #5179

Comments

@mernst
Copy link
Member

mernst commented Jun 24, 2022

Consider the following code, which appears in checker/tests/resourceleak/LemmaStack.java:

// The Resource Leak Checker issues the following error:
// LemmaStack.java:37: error: [reset.not.owning] Calling method startProver resets the must-call
// obligations of the expression this, which is non-owning. Either annotate its declaration with an
// @Owning annotation, extract it into a local variable, or write a corresponding
// @CreatesMustCallFor annotation on the method that encloses this statement.
//     startProver();
//                ^
// 1 error

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import org.checkerframework.checker.calledmethods.qual.EnsuresCalledMethods;
import org.checkerframework.checker.mustcall.qual.CreatesMustCallFor;
import org.checkerframework.checker.mustcall.qual.MustCall;
import org.checkerframework.checker.mustcall.qual.Owning;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;

@MustCall("close") public class LemmaStack implements Closeable {

  private @Owning @MustCall("close") PrintWriter session;

  @CreatesMustCallFor("this")
  @EnsuresNonNull("session")
  private void startProver() {
    try {
      if (session != null) {
        session.close();
      }
      session = new PrintWriter("filename.txt");
    } catch (IOException e) {
      throw new Error(e);
    }
  }

  public LemmaStack() {
    startProver();
  }

  @EnsuresCalledMethods(value = "session", methods = "close")
  @Override
  public void close(LemmaStack this) {
    session.close();
  }
}

None of the three suggestions in the error messages works.

  • annotate its declaration with an @Owning annotation: there is no declaration of this to annotate
  • extract it into a local variable : @Owning cannot be written on a local variable
  • write a corresponding @CreatesMustCallFor annotation on the method that encloses this statement: that does not work for the constructor.

The message should be improved to only make suggestions that are possible.
Furthermore, this false negative warning should be eliminated.

mernst added a commit that referenced this issue Jun 24, 2022
wmdietl pushed a commit to eisop/checker-framework that referenced this issue Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant