0

Guys I'm trying to fix memory leak through Debug Memory Graph and struct here.

Debug memory graph small pic

Debug memory graph big pic

My code in UICollectionView, cellForItemAt indexPath is as follow

guard let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? DiscoveryCell else {
        Log.error("DiscoveryCell not registered")
        return UICollectionViewCell()
      }
    let buttonView = self.delegate?.getBottomView(template: viewModel)
cell.configure(model: viewModel, buttonView: buttonView)
return cell

and custom cell contains

class DiscoveryCell: UICollectionViewCell, Identifiable {
private(set) var viewModel: DiscoveryProtocol?
@IBOutlet private var buttonContainerView: UIView!

func configure(model: DiscoveryProtocol, buttonView: UIView?) {
    guard let viewModel = model as? DiscoveryProtocol else {
      return
    }
    self.viewModel = viewModel

if let actionButtonView = buttonView {
      actionButtonView.frame = buttonContainerView.bounds
      self.buttonContainerView.addSubview(actionButtonView)
    }
  }

  override func prepareForReuse() {
    super.prepareForReuse()
        self.buttonContainerView.subviews.forEach { subview in
      subview.removeFromSuperview()
    }
  }
}

what's wrong with my code? I mean where do I find the memory leaks

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Al-Zubair
  • 3
  • 2
  • Probably unrelated, but is this view model a view model for this cell, or for its parent? I.e., does this cell “own” the view model, or referencing some parent view model. If it’s not the cells own view model, you’d want to make the reference `weak` or `unowned`, e.g. `private(set) weak var viewModel: DiscoveryProtocol?`. – Rob May 12 '22 at 18:31
  • 'weak' must not be applied to non-class-bound 'DiscoveryProtocol'; consider adding a protocol conformance that has a class bound – Al-Zubair May 12 '22 at 18:35
  • In answer to the question, if you turn on the “malloc stack” option, you can select an object and you can see the stack trace of where the reference was established. The doubled-headed arrow b/w the button and the closure looks suspicious, though. Did you use the `weak` references discussed in [your other question](https://stackoverflow.com/q/72214952/1271826). – Rob May 12 '22 at 18:37
  • “'weak' must not be applied to non-class-bound 'DiscoveryProtocol'” … OK, so is the object conforming to `DiscoveryProtocol` a `struct` that you you want copied at the cell? Or is it a reference type for that all the cells are sharing, then `DiscoverProtocol` should be a class protocol. But if it is a `struct` where each cell gets a copy, then no `weak` reference is appropriate. There’s not enough information above for us to know… – Rob May 12 '22 at 18:43
  • By the way, when looking at this complicated graph, focus first on the dark arrows (which indicates a strong reference). We’re less concerned about the weak references (the light gray arrows). And the “malloc stack” option should show you the stack trace to determine where the strong reference was was established. – Rob May 12 '22 at 18:50

0 Answers0