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

Add handle ownership again #876

Merged
merged 10 commits into from
Oct 4, 2022
Merged

Add handle ownership again #876

merged 10 commits into from
Oct 4, 2022

Conversation

nirinchev
Copy link
Member

@nirinchev nirinchev commented Aug 29, 2022

This is a refactoring of our native handle hierarchy. A summary of the changes:

  • Introduces a method to release the native pointer to HandleBase<T>
    • The unowned version of the handle will not attach a native finalizer and the release method is a no-op.
  • Adds RootedHandleBase<T> that is owned by a Realm and it's lifetime is at most as long as the lifetime of the parent Realm.
  • Modifies all the realm types (list, results, object, query) to inherit from RootedHandleBase and modifies their ctors to optionally pass in their parent Realm as root in case it's unowned.
    • The reason we only care about the unowned case is because that's a situation where the user doesn't have explicit control over the lifetime of the Realm instance.
  • RealmHandle cleans up all its children when released.

Supersedes #528. I'd like to add more meaningful tests but those need to wait until the migrations PR is in.

Fixes #527
Fixes #837

@nirinchev nirinchev self-assigned this Aug 29, 2022
@cla-bot cla-bot bot added the cla: yes label Aug 29, 2022
@coveralls
Copy link

coveralls commented Aug 29, 2022

Pull Request Test Coverage Report for Build 3185032259

  • 136 of 144 (94.44%) changed or added relevant lines in 8 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.7%) to 87.406%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/src/subscription.dart 3 4 75.0%
lib/src/scheduler.dart 0 2 0.0%
lib/src/native/realm_core.dart 98 103 95.15%
Totals Coverage Status
Change from base Build 3181931773: -0.7%
Covered Lines: 1971
Relevant Lines: 2255

💛 - Coveralls

@nirinchev nirinchev marked this pull request as ready for review September 6, 2022 11:21
Copy link
Contributor

@nielsenko nielsenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of the ints by using an Expando. Might be worth exploring. But otherwise LGTM.

lib/src/native/realm_core.dart Show resolved Hide resolved
lib/src/native/realm_core.dart Outdated Show resolved Hide resolved
lib/src/native/realm_core.dart Show resolved Hide resolved

final realm = getRealm(config);

expect(() => people.length, throws<RealmClosedError>());
Copy link
Contributor

@desistefanova desistefanova Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have to check also people.isManaged and people.isValid. They should't throw, right?

lib/src/native/realm_core.dart Outdated Show resolved Hide resolved
lib/src/native/realm_core.dart Outdated Show resolved Hide resolved
lib/src/native/realm_core.dart Outdated Show resolved Hide resolved
lib/src/native/realm_core.dart Outdated Show resolved Hide resolved
lib/src/native/realm_core.dart Show resolved Hide resolved
lib/src/list.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@desistefanova desistefanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we may say something in the changelog about this change?

Copy link
Contributor

@desistefanova desistefanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many places from the existing code that require two arguments, but only one is passed. Is it only on my local machine or it is because of the merge?
Please make a search for the following constructors:
RealmResultsHandle._
RealmObjectHandle._
RealmListHandle._

@nirinchev
Copy link
Member Author

Do you think we may say something in the changelog about this change?

This should be purely internal, but I can add something to the internal section.

There are many places from the existing code that require two arguments, but only one is passed.

Yeah, that happened when I merged from master via the Github UI. Will fix it before merging.

@desistefanova
Copy link
Contributor

Maybe this PR is the correct place to remove all the keepAlive methods. I think we don't need them anymore. @nielsenko ?

@nirinchev
Copy link
Member Author

The keepAlive is necessary to make sure we don't get parts of the objects finalized mid-scope. I don't think this PR does anything that would allow us to remove them.

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Desislava Stefanova <[email protected]>
@nirinchev nirinchev merged commit cc97a8a into master Oct 4, 2022
@nirinchev nirinchev deleted the ni/handle-ownership-2 branch October 4, 2022 19:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle ownership Introduce ownership semantics for our handles
5 participants