skip to Main Content

I have a problem of memory leak with a property of type Color declared in a class with SwiftUI in IOS 17.

The project I’m using it on is much more complete than the example below.
The project below is just an example with memory leaks.

I have 3 files.
The first is my class.
The second is the main view.
The third is a subview to display and modificate color.

ParamClasse

import SwiftUI

@Observable
class ParamClasse{
    
    var couleurTexte:Color = .green
    
    init(couleurTexte: Color) {
        self.couleurTexte = couleurTexte
    }
}

MainViewFile

import SwiftUI

struct MainView: View {
    
    var paramClass:ParamClasse = ParamClasse(couleurTexte: .blue)
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
                .foregroundStyle(paramClass.couleurTexte)
            
            ModifierCouleurClassView(paramClasse: paramClass)
      
        }
        .padding()
    }
}

#Preview {
    MainView()
}

ModifierCouleurClassView

import SwiftUI

struct ModifierCouleurClassView: View {
    
    @Bindable var paramClasse:ParamClasse
    
    var body: some View {
        ColorPicker("Modifier couleur texte", selection: $paramClasse.couleurTexte)
    }
}

#Preview {
    ModifierCouleurClassView(paramClasse: ParamClasse(couleurTexte: .red))
}

If I replace the class with a structure, the memory leaks are still present.

And if I use the old ways (ObservableObject and @ObservedObject) the memory leaks are also present.

Does anyone have any idea how to solve it or is it an Apple bug ?

3

Answers


  1. Chosen as BEST ANSWER

    I have try with very simple exemple, and i have always memory leak.

    With this very simple code i have memory leak. I have xcode 15.4 and macos 14.5

    If you change two time the color memory leak appears.

    File one

    import SwiftUI
    
    struct MainViewSimple: View {
        
        @State var couleurTexte:Color = .blue
        
        var body: some View {
            Text("Hello, World!")
                .foregroundStyle(couleurTexte)
            SelectColor(couleurTexte: $couleurTexte)
        }
    }
    
    #Preview {
        MainViewSimple()
    }
    

    File two

    import SwiftUI
    
    struct SelectColor: View {
        
        @Binding var couleurTexte:Color
        
        var body: some View {
            ColorPicker("couleur du texte", selection: $couleurTexte)
        }
    }
    
    #Preview {
        SelectColor(couleurTexte: .constant(.blue))
    }
    

  2. View structs shouldn’t init class objects because they have no lifetime so that var paramClass is instantly leaked (btw you should use let for params since View structs are immutable). You could use StateObject for class but State with struct is best when it’s just data and not doing async loading/saving. Nowadays you can use .task for async loading so don’t need stateobject anymore at all.

    Login or Signup to reply.
  3. Just with this i have memory leak when i change two times the color :

    import SwiftUI
    
    struct ContentView: View {
        @State var couleurTexte:Color = .blue
        
        var body: some View {
            Text("Hello, World!")
                .foregroundStyle(couleurTexte)
            ColorPicker("couleur du texte", selection: $couleurTexte)
        }
    }
    
    #Preview {
        ContentView()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search