如何修复从调试存储器图xcode中的内存泄漏

发布于 2025-01-28 08:36:54 字数 1424 浏览 0 评论 0原文

我正在尝试通过调试内存图和struct在此处修复内存泄漏。

“

“调试存储器图big

我的代码在uicollectionView中,cellforitemat indexpath如下

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

,自定义单元包含

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()
    }
  }
}

我的代码有什么问题?我的意思是在哪里找到内存泄漏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文