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 ?