Skip to content

destroy ​

Destroy the mini game runtime environment ​

destroy marks the end of the mini game lifecycle. Calling this method completely shuts down the game engine, reclaims all occupied system resources (including JavaScript virtual machine memory, render buffers, thread pools, and texture resources), and resets the instance state to unavailable.


Method Declaration ​

objectivec
/**
 * Destroy the mini game runtime environment.
 * After success, the mini game instance enters the `UNAVAILABLE` state.
 * After the call, all memory and view resources associated with this handle are released.
 */
- (void)destroy;

Parameters ​

ParameterTypeRequiredDescription
None--This method takes no parameters.

Runtime Behavior ​

  1. Resource reclamation: Forcibly stops all running JS scripts and destroys the JSC virtual machine context.
  2. Render teardown: Releases all GPU texture resources and stops the Metal/OpenGL render loop.
  3. View cleanup: The view returned by getGameView is destroyed. If the view has been added to a parent view, it is recommended to remove it manually before the call, or to nil out the reference after the call.
  4. State transition: After execution, the state becomes SUD_RT_GAME_STATE_UNAVAILABLE (0). This handle can no longer call create or start.

Example ​

objectivec
- (void)dealloc {
    [self cleanupGame];
}

- (void)cleanupGame {
    if (self.gameHandle) {
        // 1. It is recommended to stop first and notify the game layer
        [self.gameHandle stop:@"{\"reason\":\"dealloc\"}"];
        
        // 2. Fully destroy the environment
        [self.gameHandle destroy];
        
        // 3. Remove references and clean up UI
        dispatch_async(dispatch_get_main_queue(), ^{
            UIView *gameView = [self.gameHandle getGameView];
            [gameView removeFromSuperview];
            self.gameHandle = nil;
        });
    }
}

Notes ​

  • Memory leak prevention: When the ViewController is destroyed or the user exits the game scene, be sure to call destroy. Otherwise, the JS engine running in the background and cached textures will cause severe memory accumulation.
  • Irreversibility: Once destroy is executed, the current gameHandle instance is invalidated. To re-enter the game, you must create a new handle instance and run the full lifecycle again.
  • Timing: If you are only temporarily switching to the background or presenting a native interface, use pause; only use destroy when you are sure the current game instance is no longer needed.
  • Thread safety: It is recommended to initiate the destroy instruction on the main thread to keep it in sync with the UI removal operation.