Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Frozen Realm doesn't reference count correctly #7504

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

Open
alecbarber wants to merge 1 commit into realm:main
base: main
Choose a base branch
Loading
from alecbarber:patch/frozen-realm-reference-counting

Conversation

@alecbarber
Copy link

@alecbarber alecbarber commented Jun 21, 2021

Creating new instances of an already-cached version doesn't increment the reference count, which causes closing frozen Realm instances to be non-deterministic. The following code works as expected:

val realm = Realm.getDefaultInstance()
// Freeze the Realm
val realm1 = realm.freeze()
// Write to the live Realm, and freeze again
realm.executeTransaction { ... }
val realm2 = realm.freeze()
// Close the first frozen instance
realm1.close()
// Make a query on the second frozen instance
realm2.where<...>().findAll().doSomething()
// Clean up
realm2.close()
realm.close()

However, this code throws an IllegalStateException if the write operation doesn't happen between the two freeze operations:

val realm = Realm.getDefaultInstance()
// Freeze the Realm
val realm1 = realm.freeze()
// Freeze a second time first, then write to the live Realm
val realm2 = realm.freeze()
realm.executeTransaction { ... }
// Close the first frozen instance.
realm1.close()
// Make a query on the second frozen instance
// Throws IllegalStateException, saying that realm2 is closed.
realm2.where<...>().findAll().doSomething()
// Clean up
realm2.close()
realm.close()

In a use-case where frozen RealmObjects/RealmResults are being temporarily created and there are concurrent write transactions (which is for example very easy to achieve with Kotlin Coroutines), cleaning up frozen Realms rapidly becomes non-deterministic.

This PR attempts to fix the issue by making the reference-counting behaviour of frozen Realms match that of live Realms – that is, every Realm instance created with Realm.freeze() is active (and using resources) until close() is called on that instance.

Copy link
Contributor

@cla-bot check

Copy link

cla-bot bot commented Feb 22, 2022

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: alecbarber.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

Copy link

cla-bot bot commented Feb 22, 2022

The cla-bot has been summoned, and re-checked this pull request!

@alecbarber alecbarber force-pushed the patch/frozen-realm-reference-counting branch from ab0a5f0 to 84bbbcb Compare February 22, 2022 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /