diff --git a/JSCrash/entry/src/main/cpp/napi_init.cpp b/JSCrash/entry/src/main/cpp/napi_init.cpp index 955209474f0add1e875f444f27cc483853336147..403466878c5bd8117efd8eaf8d0b47ea44026270 100644 --- a/JSCrash/entry/src/main/cpp/napi_init.cpp +++ b/JSCrash/entry/src/main/cpp/napi_init.cpp @@ -1,5 +1,7 @@ #include "napi/native_api.h" + // [Start can_not_get_prototype_on_non_ecma_object_napi_case] +napi_value objvalues; static napi_value Add(napi_env env, napi_callback_info info) { size_t argc = 2; @@ -21,12 +23,32 @@ static napi_value Add(napi_env env, napi_callback_info info) napi_value sum; napi_create_double(env, value0 + value1, &sum); - + + napi_status status; + napi_handle_scope scope; + status = napi_open_handle_scope(env, &scope); + napi_value jsArray = nullptr; + napi_create_array(env, &jsArray); + // 将创建好的数组进行赋值 + for (int i = 0; i < 5; i++) { + napi_value element; + napi_create_int32(env, i, &element); + napi_set_element(env, jsArray, i, element); + } + status = napi_close_handle_scope(env, scope); // 作用域关闭,jsArray 被清理 + objvalues = jsArray; // 问题所在! + return sum; } - // [Start TestLeak] +static napi_value GetValue(napi_env env, napi_callback_info info) +{ + return objvalues; +} + // [End ocan_not_get_prototype_on_non_ecma_object_napi_case] + + // [Start out_of_memory_error_napi_case] static napi_value TestLeak(napi_env env, napi_callback_info info) { size_t argc = 1; @@ -39,7 +61,7 @@ static napi_value TestLeak(napi_env env, napi_callback_info info) // napi_delete_reference(env, task_ref->ref); return nullptr; } - // [End TestLeak] + // [End out_of_memory_error_napi_case] EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse1.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse1.ets index 54843f35d16ac66424a2bf2c3d8187cd36576438..a09ba6dffff0312c3f7b9bf3a91e066653cf45eb 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse1.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse1.ets @@ -23,7 +23,7 @@ struct Index { @State recentScale: number = 0; @State multiCardsNum: number = 0; - // [Start updateGestureValue1] + // @[Start cannot_read_property_xxx_of_undefined_case] // Update the attributes related to manual effects public updateGestureValue(screenWidth: number, recentScale: number, sceneContainerSessionList: SCBSceneContainerSession[]): void { @@ -33,7 +33,7 @@ struct Index { this.screenWidth = this.getUIContext().px2vp(screenWidth); this.recentScale = recentScale; } - // [End updateGestureValue1] + // [End cannot_read_property_xxx_of_undefined_case] build() { RelativeContainer() { diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse10.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse10.ets index b6285bdb00a1b846509f171f8930e2f53ecb44ae..d3489df5aab06ff7a7180ff75eeed5729d88bdb1 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse10.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse10.ets @@ -2,10 +2,10 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse10] +// [Start xxx_is_not_initialized_case] import { Animal } from './JSCrashCaseAnalyse11' export let a = "this is A"; export function A() { return new Animal; } -// [End JSCrashCaseAnalyse10] \ No newline at end of file +// [End xxx_is_not_initialized_case] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse11.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse11.ets index 7a07526ca7c96e04b72c0e296bb191bdd31ba798..c1553e5c84b3bb37d2372c6b613613c62680357b 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse11.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse11.ets @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse11] +// [Start xxx_is_not_initialized_case1] import { a } from './JSCrashCaseAnalyse10' export class Animal { static { @@ -10,4 +10,4 @@ export class Animal { let str = a; // 报错信息:a is not initialized } } -// [End JSCrashCaseAnalyse11] +// [End xxx_is_not_initialized_case1] diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse12.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse12.ets index 3c1c44e40f8bae92cb87189db437a976e166c645..ff8cf75c167f963ce368e662eccbd18201eb5911 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse12.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse12.ets @@ -2,10 +2,10 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse12] +// [Start xxx_is_not_initialized_fix] import { Animal } from './JSCrashCaseAnalyse11' export let a = "this is A"; export function A() { return new Animal; } -// [End JSCrashCaseAnalyse12] \ No newline at end of file +// [End xxx_is_not_initialized_fix] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse13.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse13.ets index 6bcbb2bdf8d6a38f6e1b5063b60a396e3b68874a..9d495bbcbac1aa65bb24258517d49b2197904876 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse13.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse13.ets @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse13] +// [Start xxx_is_not_initialized_fix1] import { a } from './JSCrashCaseAnalyse10' export class Animal { static { @@ -10,4 +10,4 @@ export class Animal { } str = a; // 修改点 } -// [End JSCrashCaseAnalyse13] +// [End xxx_is_not_initialized_fix1] diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse14.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse14.ets index ebcf808b4a08a09c3d2ec36fda1a3e7900597649..08031bb4e9e2cb869cc2d9df91564351a39be7b9 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse14.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse14.ets @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse14] +// [Start out_of_memory_error_case] import { hilog } from '@kit.PerformanceAnalysisKit'; import testNapi from 'libentry.so'; @@ -36,4 +36,4 @@ struct Index { function testYY() { console.log("test leak"); } -// [End JSCrashCaseAnalyse14] \ No newline at end of file +// [End out_of_memory_error_case] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse15.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse15.ets new file mode 100644 index 0000000000000000000000000000000000000000..8fe8e56fd2fc105ce1c2a033d736f0e1103ff8d5 --- /dev/null +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse15.ets @@ -0,0 +1,38 @@ +/** + * 最佳实践:JSCrash问题案例分析 + */ + +// [Start can_not_get_prototype_on_non_ecma_object_case] +import { hilog } from '@kit.PerformanceAnalysisKit'; +import testNapi from 'libentry.so'; + +declare class ArkTools { + static hintGC(): void; +} + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); + ArkTools.hintGC(); // 触发GC + // setTimeout(()=> { // 异步方法 + testNapi.getvalue().length; + // }, 1000); + + }) + } + .width('100%') + } + .height('100%') + } +} +// [End can_not_get_prototype_on_non_ecma_object_case] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse2.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse2.ets index a38af1724142de766e2ebfc5cb2ab1a9a7216076..ce2fb1c6f7db2e1be2dcddd6de71f18fafd73fe0 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse2.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse2.ets @@ -23,7 +23,7 @@ struct Index { @State recentScale: number = 0; @State multiCardsNum: number = 0; - // [Start updateGestureValue2] + // [Start cannot_read_property_xxx_of_undefined_fix] // Update the attributes related to manual effects public updateGestureValue(screenWidth: number, recentScale: number, sceneContainerSessionList: SCBSceneContainerSession[]): void { @@ -35,7 +35,7 @@ struct Index { this.screenWidth = this.getUIContext().px2vp(screenWidth); this.recentScale = recentScale; } - // [End updateGestureValue2] + // [End cannot_read_property_xxx_of_undefined_fix] build() { RelativeContainer() { diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse3.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse3.ets index bd3f649d9269cae69f3ebd51b27e5c4e05ddf9a6..95567adffacacbe806f5d5a9627c7135b2c2305d 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse3.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse3.ets @@ -21,10 +21,10 @@ struct Index { }) .onClick(()=>{ hidebug.getPss(); - // [Start TestJSError] + // [Start throw_new_error_case] // In a production environment, encrypted error codes should be used instead of plain text descriptions. throw new Error("TEST JS ERROR") - // [End TestJSError] + // [End throw_new_error_case] }) } diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse4.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse4.ets index e10d6d183a10713c71f61a824e221c7b694049bb..5461cbb8508f615164632a73a3bc64fa32055ab0 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse4.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse4.ets @@ -15,7 +15,7 @@ class Index extends BaseComponent{ handleUpdateState() {} isWifiActive: boolean = false; - // [Start JSCrashCaseAnalyse4] + // [Start operation_failed_case] onStart(): void { super.onStart(); log.showInfo('onStart'); @@ -26,5 +26,5 @@ class Index extends BaseComponent{ }); } - // [End JSCrashCaseAnalyse4] + // [End operation_failed_case] } diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse5.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse5.ets index ef57b2fa5f22db9d372536430d8bb3de556a2230..b26b20ff2223c7180bec1c1aad7fb39629740d37 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse5.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse5.ets @@ -15,7 +15,7 @@ class Index extends BaseComponent{ handleUpdateState() {} isWifiActive: boolean = false; - // [Start JSCrashCaseAnalyse5] + // [Start operation_failed_fix] onStart(): void { super.onStart(); log.showInfo('onStart'); @@ -30,5 +30,5 @@ class Index extends BaseComponent{ } } - // [End JSCrashCaseAnalyse5] + // [End operation_failed_fix] } diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse6.js b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse6.js index 9a66efe884e60a8e8157cea2d63ff7f650c69789..1bbe46450c7479ade14c3db9e652690ad1823e66 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse6.js +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse6.js @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse6] +// [Start xxx_is_not_callable_case] function b(a) { a.trim(); // 报错undefined is not callable; } @@ -10,4 +10,4 @@ export function c() { let a = 123; b(a); } -// [End JSCrashCaseAnalyse6] \ No newline at end of file +// [End xxx_is_not_callable_case] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse7.js b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse7.js index 319f443c7623a4211e01a5dafe14de99a5e7f35e..ab1c8f05392032eadcba0e8eba3cc9828a433be4 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse7.js +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse7.js @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse7] +// [Start xxx_is_not_callable_fix] function b(a) { a.trim(); } @@ -10,4 +10,4 @@ export function c() { let a = 123; b(a.toString()); // 转换为字符串 } -// [End JSCrashCaseAnalyse7] \ No newline at end of file +// [End xxx_is_not_callable_fix] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse8.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse8.ets index 8c15bc012948d968011218b23559980ec95504df..4f203f2f757d726dfe2a2ec0c2b5edcf06c58b0f 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse8.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse8.ets @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse8] +// [Start taskpool_failed_to_serialize_result_case] import taskpool from '@ohos.taskpool' import {printArgs} from './utils' import {BusinessError} from '@ohos.base' @@ -47,4 +47,4 @@ struct Index { .width('100%') } } -// [End JSCrashCaseAnalyse8] \ No newline at end of file +// [End taskpool_failed_to_serialize_result_case] \ No newline at end of file diff --git a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse9.ets b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse9.ets index ab74a81718334d704a19a73e57d691295a352209..bf93c8164a09bab22085c5d920b557c946117e6e 100644 --- a/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse9.ets +++ b/JSCrash/entry/src/main/ets/pages/JSCrashCaseAnalyse9.ets @@ -2,7 +2,7 @@ * 最佳实践:JSCrash问题案例分析 */ -// [Start JSCrashCaseAnalyse9] +// [Start taskpool_failed_to_serialize_result_fix] import taskpool from '@ohos.taskpool' import {printArgs} from './utils' import {BusinessError} from '@ohos.base' @@ -48,4 +48,4 @@ struct Index { .width('100%') } } -// [End JSCrashCaseAnalyse9] \ No newline at end of file +// [End taskpool_failed_to_serialize_result_fix] \ No newline at end of file