Remove node from ARSCNView cause random crash(btCollisionWorld::updateSingleAabb)

I made a ball drop animation on the ARSCNView, then after 1 second, then deleted the ball and re-added it to the original position on the view, but this occasionally crashes.

something like this:

    private func dropBall(){

        // do ball dropping animation
        let randomX = Float.random(in: -2...2)  * 0.001
        let randomY = Float.random(in: 3...10)  * 0.001
        ballNode?.physicsBody =  SCNPhysicsBody(type: .dynamic, shape: nil)
        ballNode?.physicsBody?.mass = 0.005
        let position = SCNVector3(x: 0.05, y: 0.05, z: 0.05)
        let force = SCNVector3(x: randomX, y: randomY , z: 0)
        ballNode?.physicsBody?.applyForce(force, at: position, asImpulse: true)
        let tv = SCNVector4(x: -0.2 + Float(0.4 * randomCGFloat()), y: 0 , z: 0.2, w: 0)
        ballNode?.physicsBody?.applyTorque(tv, asImpulse: true)
        
        //remove the ball 
        DispatchQueue.main.asyncAfter(deadline: .now() +  1){
            self.ballNode?.physicsBody = nil
            
            self.ballNode?.removeFromParentNode()
            self.ballNode = nil
            // add ball again
            let ballNode = SCNReferenceNode(named: "myscn.scn")
            contentNode?.addChildNode(ballNode)
            self.ballNode = ballNode

        }
    }

crash stack:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000068
Exception Codes: 0x0000000000000001, 0x0000000000000068
VM Region Info: 0x68 is not in any region.  Bytes before following region: 68719476632
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      commpage (reserved)     1000000000-7000000000 [384.0G] ---/--- SM=NUL  ...(unallocated)
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [30984]

Triggered by Thread:  11


Thread 11 name:
Thread 11 Crashed:
0   SceneKit                      	0x00000001cf3ef998 btDbvtBroadphase::setAabb(btBroadphaseProxy*, btVector3 const&, btVector3 const&, btDispatcher*) + 52
1   SceneKit                      	0x00000001cf65126c btCollisionWorld::updateSingleAabb(btCollisionObject*) + 284
2   SceneKit                      	0x00000001cf65133c btCollisionWorld::updateAabbs() + 56
3   SceneKit                      	0x00000001cf3ff39c btCollisionWorld::performDiscreteCollisionDetection() + 32
4   SceneKit                      	0x00000001cf406964 btDiscreteDynamicsWorld::internalSingleStepSimulation(float) + 120
5   SceneKit                      	0x00000001cf3ff574 btDiscreteDynamicsWorld::stepSimulation(float, int, float) + 276
6   SceneKit                      	0x00000001cf507ad8 -[SCNPhysicsWorld _step:] + 156 (SCNPhysicsWorld.mm:1423)
7   SceneKit                      	0x00000001cf3ae9fc -[SCNRenderer _update:] + 932 (SCNRenderer.m:3814)
8   SceneKit                      	0x00000001cf3ae364 -[SCNRenderer _drawSceneWithNewRenderer:] + 152 (SCNRenderer.m:5303)
9   SceneKit                      	0x00000001cf3ae278 -[SCNRenderer _drawScene:] + 40 (SCNRenderer.m:5537)
10  SceneKit                      	0x00000001cf3a78b4 -[SCNRenderer _drawAtTime:] + 500 (SCNRenderer.m:5727)
11  SceneKit                      	0x00000001cf3a74cc -[SCNView _drawAtTime:] + 368 (SCNView.m:1542)
12  SceneKit                      	0x00000001cf4ba478 __83-[NSObject(SCN_DisplayLinkExtensions) SCN_setupDisplayLinkWithQueue:screen:policy:]_block_invoke + 44 (SCNDisplayLink_ARC.m:42)
13  SceneKit                      	0x00000001cf595fa0 -[SCNDisplayLink _displayLinkCallbackReturningImmediately] + 148 (SCNDisplayLink.m:381)
14  libdispatch.dylib             	0x0000000190a267c8 _dispatch_client_callout + 16 (object.m:560)
15  libdispatch.dylib             	0x00000001909fde7c _dispatch_continuation_pop$VARIANT$armv81 + 436 (inline_internal.h:2632)
16  libdispatch.dylib             	0x0000000190a0f860 _dispatch_source_invoke$VARIANT$armv81 + 1552 (source.c:596)
17  libdispatch.dylib             	0x0000000190a0172c _dispatch_lane_serial_drain$VARIANT$armv81 + 308 (inline_internal.h:0)
18  libdispatch.dylib             	0x0000000190a022e4 _dispatch_lane_invoke$VARIANT$armv81 + 380 (queue.c:3940)
19  libdispatch.dylib             	0x0000000190a0c000 _dispatch_workloop_worker_thread + 612 (queue.c:6846)
20  libsystem_pthread.dylib       	0x00000001d0f3cb50 _pthread_wqthread + 284 (pthread.c:2618)
21  libsystem_pthread.dylib       	0x00000001d0f3c67c start_wqthread + 8

How to fix this? This will only crash by chance and is not too easy to reproduce, is there a problem with me deleting this node in this way?

Replies

imo "occasional crash" almost always means "race condition". Look at refactoring your code to add and remove objects in the - (void)renderer:(id )renderer updateAtTime:(NSTimeInterval)time callback?

  • Thanks to your time, I finally found the problem: I am using ARKIT for face tracking , sometimes face is not tracked, at the moment, doing the add/remove node caused the crash.

Add a Comment