admin管理员组

文章数量:1315967

I have a util helper that adds physics body to a node:

  @discardableResult
  public static func addPhysicsBody(to node: SCNNode, type: SCNPhysicsBodyType) -> SCNPhysicsBody {
    // Here I use SCNPhysicsShape(node:) API
    let shape = SCNPhysicsShape(node: node)
    let body = SCNPhysicsBody(type: type, shape: shape)
    node.physicsBody = body
    return body
  }

There seems to be a retain cycle. Even if I remove the node from parent, the node is still in memory.

However, if I change the code to:

  @discardableResult
  public static func addPhysicsBody(to node: SCNNode, type: SCNPhysicsBodyType) -> SCNPhysicsBody {
    // Here I change to SCNPhysicsShape(geometry:) API
    let shape =  SCNPhysicsShape(geometry: node.geometry!)
    let body = SCNPhysicsBody(type: type, shape: shape)
    node.physicsBody = body
    return body
  }

This solves the retain cycle - after node is removed from parent, the node is not in memory anymore.

I suspect the retain cycle is caused by SCNPhysicsShape(node: node) API ((node:options:)), where the node retains physics body, which retains shape, which retains the node.

However, I feel dumbfounded that apple didn't realize such an obvious retain cycle, which makes me doubt myself. Did I use the API wrong?

本文标签: iosRetain cycle in SCNNode39s physicsBodyStack Overflow