建议在Component中新建自定义节点时主动释放节点,避免因未释放节点导致的内存泄漏。
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) // 创建 BuilderNode
})
Button('close dialog')
.onClick(() => {
if (this.builder) {
this.builder.dispose() // 释放构建出的节点
this.builder = null
}
})
}
.width('100%')
.height('100%')
.padding(20)
.backgroundColor(Color.Grey)
}
} 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();
// 没有释放旧 builder,直接创建新 builder
this.builder = new BuilderNode(context)
})
}
.width('100%')
.height('100%')
.padding(20)
.backgroundColor(Color.Grey)
}
}