0

I was working on an iOS project where Realm sdk was used. When I checked the leaks instrument of Xcode, it kept on giving me memory leaks on creation of Realm instance. Then I created a very simple demo app using swiftui and Realm. I looked into memory leaks again and I see the leaks again. I want to know is there a way to get rid of these leaks cuz I checked on internet but there is no such solution.

Memory leak on app start

View to show names list:

 import SwiftUI
 import RealmSwift

 struct NameListView: View {

     @ObservedResults(NameModel.self) var nameList

     var body: some View {
    
         List(nameList) { nameObj in
        Text(nameObj.name)
         }
     }
 }

Name model:

 class NameModel: Object, ObjectKeyIdentifiable {

     @Persisted(primaryKey: true) var id: Int
     @Persisted var name: String
 }

View to add names to Realm

 struct AddNameView: View {

     @State private var name: String = ""
     @ObservedResults(NameModel.self) var nameList

     var body: some View {
    
         HStack {
             TextField("Enter name here...", text: $name)
                 .textFieldStyle(.roundedBorder)

             Button {
                 let obj = NameModel()
                 obj.name = name
                 obj.id = nameList.count
                 $nameList.append(obj)
             } label: {
                 Text("Add")
                     .padding()
             }
         }
         .padding()
     }
 }

And this is my Home view

 struct HomeView: View {
     var body: some View {
    
         VStack{
             NameListView()
                 .padding(.bottom, 5)
             AddNameView()
                 .frame(height: 50)
         }
     }
 }

And the screen looks like this

App screen

  • Showing us a memory leak doesn't really provide enough information about the cause. You need to include the code and your troubleshooting so we can fully understand the question. Please take a moment and review [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Jay Mar 17 '22 at 21:22
  • @Jay added the code – Rajat Dhasmana Mar 19 '22 at 08:05
  • Interesting. I copy and pasted your code into a project and I am not seeing that. The versions of what's being used were not included so ensure everything is updated. Unfortunately, there is nothing we can do here to help with a third party memory leak issue - we can only assist with code you wrote. I think your best bet is to file a bug report on Realm's [Github repository](https://github.com/realm/realm-swift/issues) with the same info. – Jay Mar 19 '22 at 14:19

0 Answers0