realm-core icon indicating copy to clipboard operation
realm-core copied to clipboard

Object has been deleted or invalidated.

Open xiles opened this issue 2 years ago • 11 comments

SDK and version

SDK : Swift Version: Realm: 10.28.6 RealmDatabase: 12.5.1

App crashes when this code runs.

let realm = try Realm()
let fetchResult = realm.objects(HLAsset.self).filter("assetCollection=%@", hlAssetCollection)

for i in 0 ..< fetchResult!.count {
    print(fetchResult![i].identifier) // => crash (not crashed for all object. explained below)
}

App was working well (database was OK). Added some objects to database. (nothing special) Exited an app. App runs => crash

CleanShot 2022-08-29 at 13 13 46@2x CleanShot 2022-08-29 at 13 16 18@2x CleanShot 2022-08-29 at 13 16 42@2x

You can see something is wrong with this screenshot. When I open the realm file with Realm Studio, there are objects have same identifier(Primary Key). I don't know how this can happen. When reading other objects, there is no problem. But when reading objects like this (some objects have same identifier), app crashes.

CleanShot 2022-08-29 at 13 18 05@2x

When I tried to save data as JSON, it failed. When I tried to save data as Local Realm, it saved .realm file(size changed from 167.1MB to 23.1MB) but it is not opened with Realm Studio nor an app.

xiles avatar Aug 29 '22 06:08 xiles

I can confirm I have seen this kind of crash-report in a similar use-case as well.

BlueCobold avatar Aug 29 '22 11:08 BlueCobold

Transferring to realm-swift as this seem to be happening above the Core line.

jedelbo avatar Aug 29 '22 12:08 jedelbo

I'm not sure this is true, @jedelbo. I have seen this kind of crash/report also on Android and traced it down to lines that basically did nothing more than shown in this report.

BlueCobold avatar Aug 29 '22 12:08 BlueCobold

@xiles Can you reduce this issue into a small sample app that reproduces the crash?

leemaguire avatar Aug 29 '22 12:08 leemaguire

@leemaguire I sent an email to [email protected] with a broken database file. I cannot make a sample app to break a database file.. I don't know why and when it happened. But if you open a broken database file then select several objects has same identifier(Primary Key) and delete, Realm Studio says "Object is invalid. Either it has been previously deleted or the Realm it belongs to has been closed.”

xiles avatar Aug 30 '22 01:08 xiles

We will investigate why such a scenario occurs.

leemaguire avatar Aug 31 '22 22:08 leemaguire

@leemaguire I can investigate.

jedelbo avatar Sep 01 '22 07:09 jedelbo

The database has some references pointing in the wrong direction. So it is clearly broken. I will take the issue back to core and make some further analysis on the file.

jedelbo avatar Sep 01 '22 08:09 jedelbo

Why got this issue closed? It's analysed, but not fixed.

BlueCobold avatar Sep 01 '22 09:09 BlueCobold

@BlueCobold Thanks for mentioning! Something got messed up when the issue was transferred from the Swift Repo.

bmunkholm avatar Sep 01 '22 16:09 bmunkholm

Internal note: The database has invalid entries in the following clusters: [1, 2, 2, 4, 204] [1, 2, 2, 4, 206] [1, 2, 2, 4, 207] The keys array at [1,2,2,4] is also invalid.

jedelbo avatar Sep 05 '22 13:09 jedelbo

@bmunkholm Can you tell which realm swift version fixes the issue?

keerthika-priya avatar Nov 24 '22 05:11 keerthika-priya

@keerthika-priya It's not released yet. The fix/pr is in realm-core 12.12.0, which is not yet included in a released realm-swift version: https://github.com/realm/realm-swift/blob/master/CHANGELOG.md

BlueCobold avatar Nov 24 '22 11:11 BlueCobold

@bmunkholm , @dianaafanador3 Do you know when the release can be expected for this issue?

keerthika-priya avatar Nov 24 '22 16:11 keerthika-priya

@keerthika-priya No. I'm not working for Realm/Mongo and I'm waiting for that release myself.

BlueCobold avatar Nov 24 '22 17:11 BlueCobold

@BlueCobold @BlueCobold We sure gonna do a release next week.

dianaafanador3 avatar Nov 25 '22 10:11 dianaafanador3

@dianaafanador3 any updates regarding the release?

keerthika-priya avatar Dec 01 '22 15:12 keerthika-priya

@BlueCobold @keerthika-priya Version v10.33.0 https://github.com/realm/realm-swift/releases/tag/v10.33.0 has been released with the fix

dianaafanador3 avatar Dec 02 '22 07:12 dianaafanador3

Has the new release fixed the issue for everyone? Does it require any additional steps to stop the crashing?

keerthika-priya avatar Dec 25 '22 19:12 keerthika-priya