创建XComponent后调用getXComponentSurfaceId方法出现Crash异常,并且onSurfaceCreated方法没有被执行,问题代码如下:
@Entry @Component struct Problem { @State xcArray: CustomXComponentController[] = []; aboutToAppear(): void { for (let i = 0; i < 2; i++) { this.xcArray.push(new CustomXComponentController()); } } build() { Column() { XScreen({ xcArray: this.xcArray }); } .height('100%') .width('100%'); } } @Component struct XScreen { @Prop xcArray: CustomXComponentController[]; build() { Column() { ForEach(this.xcArray, (xc: CustomXComponentController, index: number) => { Button('ButtonXc:' + index) .onClick(() => { console.info(`getXComponentSurfaceId = ${xc.getXComponentSurfaceId()}`); }); XComponent({ id: 'XComponent' + index, type: XComponentType.SURFACE, controller: xc }) .width('100%') .height(200); }); }.width('100%') .height('100%'); } } class CustomXComponentController extends XComponentController { onSurfaceCreated(surfaceId: string): void { console.info(`onSurfaceCreated surfaceId: ${surfaceId}`); } onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { console.info(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); } onSurfaceDestroyed(surfaceId: string): void { console.info(`onSurfaceDestroyed surfaceId: ${surfaceId}`); } }
Crash日志如下:
Process name:com.example.xxx Process life time:3s Reason:Signal:SIGSEGV(SEGV_MAPERR)@0x0000000000000018 probably caused by NULL pointer dereference Fault thread info: Tid:29620, Name:250918175335021 #00 pc 0000000000e2c300 /system/lib64/platformsdk/libace_compatible.z.so(OHOS::Ace::Framework::JSXComponentController::GetSurfaceId(OHOS::Ace::Framework::JsiCallbackInfo const&)+8)(eab4f4be93e4c3c88d2272a46b36c765) #01 pc 0000000000e2ca38 /system/lib64/platformsdk/libace_compatible.z.so(panda::Local<panda::JSValueRef> OHOS::Ace::Framework::JsiClass<OHOS::Ace::Framework::JSXComponentController>::InternalJSMemberFunctionCallback<OHOS::Ace::Framework::JSXComponentController>(panda::JsiRuntimeCallInfo*)+256)(eab4f4be93e4c3c88d2272a46b36c765) #02 pc 0000000000589f1c /system/lib64/platformsdk/libark_jsruntime.so(panda::Callback::RegisterCallback(panda::ecmascript::EcmaRuntimeCallInfo*)+272)(38230492305256c23dcf27a2aba6f771) #03 pc 0000000000445a2c /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40) #04 at anonymous (entry/src/main/ets/pages/XScreen.ets:22:49)
aboutToAppear(): void { for (let i = 0; i < 2; i++) { this.xcArray.push(new CustomXComponentController()) } }
@Prop装饰变量时会进行深拷贝,子页面渲染完成后xcArray对象还未传递完成,会导致XComponent对象创建异常,因此使用getXComponentSurfaceId方法出现闪退,具体原理可以参考@Prop修饰的值没有及时更新如何解决。
@Entry @Component struct Solution1 { xcArray: CustomXComponentController1[] = []; aboutToAppear(): void { for (let i = 0; i < 2; i++) { this.xcArray.push(new CustomXComponentController1()); } } build() { Column() { XScreen1({ xcArray: this.xcArray }); } .height('100%') .width('100%'); } } @Component struct XScreen1 { xcArray: CustomXComponentController1[] = []; build() { Column() { ForEach(this.xcArray, (xc: CustomXComponentController1, index: number) => { Button('ButtonXc:' + index) .onClick(() => { console.info(`getXComponentSurfaceId = ${xc.getXComponentSurfaceId()}`); }); XComponent({ id: 'XComponent' + index, type: XComponentType.SURFACE, controller: xc }) .width('100%') .height(200); }); }.width('100%') .height('100%'); } } class CustomXComponentController1 extends XComponentController { onSurfaceCreated(surfaceId: string): void { console.info(`onSurfaceCreated surfaceId: ${surfaceId}`); } onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { console.info(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); } onSurfaceDestroyed(surfaceId: string): void { console.info(`onSurfaceDestroyed surfaceId: ${surfaceId}`); } }
@Entry @Component struct Solution2 { build() { Column() { XScreen2(); } .height('100%') .width('100%'); } } @Component struct XScreen2 { xcArray: CustomXComponentController2[] = []; aboutToAppear(): void { for (let i = 0; i < 2; i++) { this.xcArray.push(new CustomXComponentController2()); } } build() { Column() { ForEach(this.xcArray, (xc: CustomXComponentController2, index: number) => { Button('ButtonXc:' + index) .onClick(() => { console.info(`getXComponentSurfaceId = ${xc.getXComponentSurfaceId()}`); }); XComponent({ id: 'XComponent' + index, type: XComponentType.SURFACE, controller: xc }) .width('100%') .height(200); }); }.width('100%') .height('100%'); } } class CustomXComponentController2 extends XComponentController { onSurfaceCreated(surfaceId: string): void { console.info(`onSurfaceCreated surfaceId: ${surfaceId}`); } onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { console.info(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); } onSurfaceDestroyed(surfaceId: string): void { console.info(`onSurfaceDestroyed surfaceId: ${surfaceId}`); } }
@Entry @Component struct Solution3 { @State xcArray: CustomXComponentController3[] = []; aboutToAppear(): void { for (let i = 0; i < 2; i++) { this.xcArray.push(new CustomXComponentController3()); } } build() { Column() { XScreen3({ xcArray: this.xcArray }); } .height('100%') .width('100%'); } } @Component struct XScreen3 { @Link xcArray: CustomXComponentController3[]; build() { Column() { ForEach(this.xcArray, (xc: CustomXComponentController3, index: number) => { Button('ButtonXc:' + index) .onClick(() => { console.info(`getXComponentSurfaceId = ${xc.getXComponentSurfaceId()}`); }); XComponent({ id: 'XComponent' + index, type: XComponentType.SURFACE, controller: xc }) .width('100%') .height(200); }); }.width('100%') .height('100%'); } } class CustomXComponentController3 extends XComponentController { onSurfaceCreated(surfaceId: string): void { console.info(`onSurfaceCreated surfaceId: ${surfaceId}`); } onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { console.info(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); } onSurfaceDestroyed(surfaceId: string): void { console.info(`onSurfaceDestroyed surfaceId: ${surfaceId}`); } }