目录

@performance/custom-node-memory-leak-check

(Recommended) Proactively release nodes when creating custom nodes in the component to prevent memory leakage caused by unreleased nodes.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@performance/custom-node-memory-leak-check": "warn",
  }
}

Options

This rule is not configurable.

Example of Correct Code

import { BuilderNode } from '@kit.ArkUI';

@Entry
@Component
struct BuilderNodeDisposeExample {
  private builder: BuilderNode<[]> | null = null
  build() {
    Column({ space: 20 }) {
      Button('open dialog')
        .onClick(() => {
          const uiContext = this.getUIContext()
          this.builder = new BuilderNode(uiContext) // Create a BuilderNode.
        })

      Button('close dialog')
        .onClick(() => {
          if (this.builder) {
            this.builder.dispose() // Release the constructed node.
            this.builder = null
          }
        })
    }
    .width('100%')
    .height('100%')
    .padding(20)
    .backgroundColor(Color.Grey)
  }
}

Example of Incorrect Code

import { BuilderNode } from '@kit.ArkUI';

@Entry
@Component
struct LeakyBuilderExample {
  private builder: BuilderNode<[]> | null = null
  build() {
    Column({ space: 20 }) {
      Button('create dialog')
        .onClick(() => {
          const context = this.getUIContext();

          // Create a new builder without releasing the old builder.
          this.builder = new BuilderNode(context)

        })
    }
    .width('100%')
    .height('100%')
    .padding(20)
    .backgroundColor(Color.Grey)
  }
}

Rule Set

plugin:@performance/all

For details about how to configure Code Linter check rules, see Code Linter.