22.2 C
Canberra
Monday, February 24, 2025

ios – Retain cycle in SCNNode’s physicsBody


I’ve a util helper that provides physics physique to a node:

  @discardableResult
  public static func addPhysicsBody(to node: SCNNode, sort: SCNPhysicsBodyType) -> SCNPhysicsBody {
    // Right here I exploit SCNPhysicsShape(node:) API
    let form = SCNPhysicsShape(node: node)
    let physique = SCNPhysicsBody(sort: sort, form: form)
    node.physicsBody = physique
    return physique
  }

There appears to be a retain cycle. Even when I take away the node from guardian, the node remains to be in reminiscence.

Nonetheless, if I modify the code to:

  @discardableResult
  public static func addPhysicsBody(to node: SCNNode, sort: SCNPhysicsBodyType) -> SCNPhysicsBody {
    // Right here I modify to SCNPhysicsShape(geometry:) API
    let form =  SCNPhysicsShape(geometry: node.geometry!)
    let physique = SCNPhysicsBody(sort: sort, form: form)
    node.physicsBody = physique
    return physique
  }

This solves the retain cycle – after node is faraway from guardian, the node is just not in reminiscence anymore.

I think the retain cycle is brought on by SCNPhysicsShape(node: node) API (https://developer.apple.com/documentation/scenekit/scnphysicsshape/init(node:choices:)), the place the node retains physics physique, which retains form, which retains the node.

Nonetheless, I really feel dumbfounded that apple did not understand such an apparent retain cycle, which makes me doubt myself. Did I exploit the API incorrect?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles