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: false positive from ownership transfer during re-assignment #5971

Closed
kelloggm opened this issue May 31, 2023 · 0 comments · Fixed by #5972
Closed

Resource Leak Checker: false positive from ownership transfer during re-assignment #5971

kelloggm opened this issue May 31, 2023 · 0 comments · Fixed by #5972
Assignees

Comments

@kelloggm
Copy link
Contributor

kelloggm commented May 31, 2023

This is a sub-issue of #5969 (where it is described as false positive 1). Consider the following code, which I've checked into the cm-ownership-transfer-at-reassignment branch of my fork:

import org.checkerframework.checker.calledmethods.qual.EnsuresCalledMethods;
import org.checkerframework.checker.mustcall.qual.CreatesMustCallFor;
import org.checkerframework.checker.mustcall.qual.InheritableMustCall;
import org.checkerframework.checker.mustcall.qual.Owning;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.IOException;

@InheritableMustCall("disconnect")
public class OwnershipTransferAtReassignment {

  private @Owning @Nullable Node head = null;

  @CreatesMustCallFor("this")
  public boolean add() {
    head = new Node(head);
    return true;
  }

  @EnsuresCalledMethods(value="this.head", methods="disconnect")
  public void disconnect() {
    head.disconnect();
  }

  @InheritableMustCall("disconnect")
  private static class Node {
    @Owning private final @Nullable Node next;

    public Node(@Owning @Nullable Node next) {
      this.next = next;
    }

    @EnsuresCalledMethods(value="this.next", methods="disconnect")
    public void disconnect() {
      next.disconnect();
    }
  }
}

Currently, the checker issues a warning when head is re-assigned at line 16. No warnings should be issued (the re-assignment is safe, because ownership is transferred during the re-assignment to the Node constructor).

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

Successfully merging a pull request may close this issue.

2 participants