追記
- 日本語か、もしくはアルファベットでも10文字以上だと発生
環境
- MacOS Big Sur
- Xcode 13.1
- Swift5
GitHub
現象
struct LoginView: View {
@ObservedObject private var viewModel: LoginViewModel
init(viewModel: LoginViewModel) {
self.viewModel = viewModel
}
var body: some View {
VStack {
Text(viewModel.invalidMessage)
.foregroundColor(.red)
.accessibility(identifier: "loginInvalidMessage")
TextField("Eメール", text: $viewModel.email)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.padding()
.accessibility(identifier: "loginEmailTextField")
SecureField("パスワード", text: $viewModel.password)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.padding()
.accessibility(identifier: "loginPasswordSecureField")
// これがメモリリークする
Button(action: {}) {
Text("ログイン")
.frame(maxWidth: .infinity)
.padding()
.foregroundColor(.white)
.background(Color(.orange))
.cornerRadius(24)
.padding()
.accessibility(identifier: "loginButton")
}
// 追記: これもメモリリークする
NavigationLink(destination: signUpView) {
Text("新規登録")
.frame(maxWidth: .infinity)
.padding()
.foregroundColor(.white)
.background(Color(.orange))
.cornerRadius(24)
.padding()
.accessibility(identifier: "toSignUpButton")
}
}
}
}
文字を入れるとメモリリークする
Button(action: {}) {
Text("ログイン")
.frame(maxWidth: .infinity)
.padding()
.foregroundColor(.white)
.background(Color(.orange))
.cornerRadius(24)
.padding()
.accessibility(identifier: "loginButton")
}
空文字だとメモリリークしない
Button(action: {}) {
Text("")
.frame(maxWidth: .infinity)
.padding()
.foregroundColor(.white)
.background(Color(.orange))
.cornerRadius(24)
.padding()
.accessibility(identifier: "loginButton")
}
やったこと
TextではなくImageを使うとメモリリークしなくなった。
さすがにおかしいので何か知っている方いらっしゃれば教えてください。
追記: 10文字未満のアルファベットを指定するとメモリリークしなくなったのでひとまず日本語を使わなければ大丈夫そう
Button(action: {}) {
Image(systemName: "hand.thumbsup.fill")
}
Button(action: { viewModel.didTapLoginButton.send() }) {
Text("Login")
.padding()
.foregroundColor(.white)
.background(Color(.orange))
.cornerRadius(24)
.padding()
.opacity(viewModel.isLoginEnabled ? 1 : 0.5)
.accessibility(identifier: "loginButton")
}.disabled(!viewModel.isLoginEnabled)