realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

RealmSwift: List and LinkingObjects in Realm object causes memory leaks. #5277

Closed aKentaroIchima closed 7 years ago

aKentaroIchima commented 7 years ago

Goals

Remove memory leaks in app.

Expected Results

Memory must be freed including List or LinkingObjects.

Actual Results

When I profile our app with Xcode 8.3.3 Profiler, many leaks is reported. Actually, our app crashes by memory allocation failure in Realm.

Steps to Reproduce

Make object written in Code Sample and instantiate those objects from Realm query results. Leaked object is List<> object self. It is constructed from Objective-C and used in Swift.

Code Sample

Following Objects cause leaks.

class UnitObject: Object {
    let users = List<UserObject>()
}
class UserObject: Object {
    let units = LinkingObjects(fromType: UnitObject.self, property: "users")
}

Version of Realm and Tooling

Swift version: 3.1

Realm framework version:

Xcode version: 8.3.3

iOS/OSX version: 8-10

austinzheng commented 7 years ago

Thanks for getting in touch with us. Can you provide us with a reproduction case that we can run in Instruments to see instances where the types you mentioned are leaking memory?

aKentaroIchima commented 7 years ago

I found the similar issue: https://github.com/realm/realm-cocoa/issues/4334 The sample code in that issue causes the same leaks. Please see https://github.com/joninsky/Realm-Memory-Test

You can see the leaks by Xcode memory profiler. List<Friends> and LinkingObjects<User> are target. It seems that objects in Realm object is not freed even if Realm object was freed.

bdash commented 7 years ago

Duplicate of #4334.