Problem with UIBarButton menu

I push (show segue) from root view controller to VC2. VC2 has the below code for setting a menu for a bar button item.

If I comment out the last line, "editButton.menu = editMenu", CPU usage is stable.

Otherwise, each time VC2 is displayed, CPU usage continues to increase and also some calculations become wrong.

Same code is also present in the root view controller and in the root view controller it works fine without commenting out the problem line.

@IBOutlet weak var editButton: UIBarButtonItem!

In viewDidLoad:

var editmenuItems : [UIAction] {
                return [
                    UIAction(title: "Edit Value", image: UIImage(systemName: "square.and.pencil"), handler: {
                        (_) in
                        self.edit()
                    }),
                    UIAction(title: "Fill Values", image: UIImage(systemName: "ellipsis.rectangle"), handler: {
                        (_) in
                        self.fill()
                    })
                ]
}          
var editMenu: UIMenu {
        return  UIMenu(children: editmenuItems)
 } 
 editButton.menu = editMenu // this line causes CPU increase in VC2.

I am using Xcode 14.2, iPhone only app, minimum iOS 15.0

What happens if you don't use the computed var but change

editButton.menu = editMenu

to

editButton.menu = UIMenu(children: editmenuItems)

Claude31,

Thanks. But it does not help either.

Complete viewDidLoad does not have any other lines related to this.

But, if I comment out self.edit()and self.fill() and uncomment editButton.menu = editMenu, then the CPU and wrong calculation problems go away too.

var editmenuItems : [UIAction] {
                return [
                    UIAction(title: "Edit Value", image: UIImage(systemName: "square.and.pencil"), handler: {
                        (_) in
                        // self.edit()
                        self.test()
                    }),
                    UIAction(title: "Fill Values", image: UIImage(systemName: "ellipsis.rectangle"), handler: {
                        (_) in
                        // self.fill()
                        self.test()
                    })
                ]
}       

and

func test() {
    print("Test")
}

self is required by the compiler.

Ok. I tried your suggestion. Still the CPU and wrong calculation problems continue.

Putting statements instead of function call does not cause CPU or wrong calculation problems. But it is not practical since some of my functions are long.

var editmenuItems : [UIAction] {
                return [
                    UIAction(title: "Edit Value", image: UIImage(systemName: "square.and.pencil"), handler: {
                        (_) in
                        // self.test() // function call causes problems
                        print("test" // no problem
                    }),
                    UIAction(title: "Fill Values", image: UIImage(systemName: "ellipsis.rectangle"), handler: {
                        (_) in
                        // self.test() // function causes problems
                        print("test" // not problem
                    })
                ]
}   

where

func test() {
    print("test")
}

Claude31, the class is long.

However the root controller has similar code and the menus do not cause any problem on the root view controller.

There are also other view controllers that are pushed from root view controller and bar button menu(s) causes the same CPU and wrong calculation problems there too.

It seems related with the push (segue) from the root view controller to other view controllers.
With each back to root and push to other view controller CPU load and wrong calculation amount increase.

I both tried putting the bar button through storyboard and programmatically, but it did not matter in either case.

Claude31,

I included the [self] and it did not solve the problem.

In fact if the handler code does not include any class variables then the code works without CPU problems.

For example, if vGlobal is a global variable and vLocal is a local variable

import Foundation
import UIKit

var vGlobal = 0

class ViewController: UIViewController {

var vLocal = 0
.
.

override func viewDidLoad {
   super.viewDidLoad()
.
.

   UIAction(title: "Edit Value", image: UIImage(systemName: "square.and.pencil"),        handler: {
           (_) in
           print(self.vLocal) // problem
           print(vGlobal) // no problem
 })
.
.
}
.
.
}

Writing the code like the below seems to resolve the CPU and wrong calculation issues at first impression. I need to do extensive tests to see if the calculations are really correct. Thank you Claude31 for your help about the [self] clause.

var editmenuItems : [UIAction] {
                return [
                    UIAction(title: "Edit Value", image: UIImage(systemName: "square.and.pencil"), handler: { [weak self]
                        (_) in
                        self?.edit()
                    }),
                    UIAction(title: "Fill Values", image: UIImage(systemName: "ellipsis.rectangle"), handler: { [weak self]
                        (_) in
                        self?.fill()
                    })
                ]
}          
var editMenu: UIMenu {
        return  UIMenu(children: editmenuItems)
} 
editButton.menu = editMenu
Problem with UIBarButton menu
 
 
Q