{"id":15141736,"url":"https://github.com/nativescript/marking-mode-example","last_synced_at":"2026-02-17T10:07:03.432Z","repository":{"id":141978071,"uuid":"169412681","full_name":"NativeScript/marking-mode-example","owner":"NativeScript","description":"An example demonstrating how to enable \"markingMode: none\" and fix a common error related to its usage.","archived":false,"fork":false,"pushed_at":"2019-02-07T15:03:12.000Z","size":2680,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-19T00:45:49.501Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NativeScript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-02-06T13:45:07.000Z","updated_at":"2019-02-07T15:03:14.000Z","dependencies_parsed_at":"2023-09-23T22:33:58.162Z","dependency_job_id":null,"html_url":"https://github.com/NativeScript/marking-mode-example","commit_stats":{"total_commits":4,"total_committers":1,"mean_commits":4.0,"dds":0.0,"last_synced_commit":"4829db7701105b97cce713d831b33497f071e75e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NativeScript/marking-mode-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fmarking-mode-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fmarking-mode-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fmarking-mode-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fmarking-mode-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NativeScript","download_url":"https://codeload.github.com/NativeScript/marking-mode-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fmarking-mode-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278794401,"owners_count":26046968,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-26T09:01:32.749Z","updated_at":"2025-10-07T14:50:41.208Z","avatar_url":"https://github.com/NativeScript.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# An app with markingMode:none enabled\n\nThis repo includes a {N} app with `markingMode: none` enabled. It reporoduces 2 common errors related to using this feature. \n\n## To make it crash\n\n1. Run the app:\n```bash\ntns run android\n```\n2. Click on `ADD BUTTON WITH CLICK LISTENER` button to create a native Android button with a native `click` callback.\n3. Click the generated button intermittently (until crash).\n\nAt some time (within a minute) the app will crash with either of the following errors:\n- `Error: com.tns.NativeScriptException: Attempt to use cleared object reference id=\u003csome-object-id-number\u003e`\n- `The JavaScript instance no longer has available Java instance counterpart`\n\n## To fix it\n\nFind the \"UNCOMMENT THE FIX\" lines in `home-page.ts` and uncomment them.\n\n## But wait, what happened? (explanation)\n\nConsider this method from `home-page.ts`:\n\n```js\npublic onAddClickListener() {\n    let root = \u003cStackLayout\u003ecurrentPage.getViewById('root');\n    let btn = new android.widget.Button(root._context);\n    btn.setText(\"ta-daa, now click!\");\n    root.android.addView(btn);\n\n    let file = new java.io.File('real file');\n\n    btn.setOnClickListener(new android.view.View.OnClickListener(\n        {\n            onClick: () =\u003e {\n                this.fileName = `${file.getName()} exists at ${new Date().toTimeString()}`;\n            }\n        }\n    ));\n}\n```\n\nIf you look in `onAddClickListener()` handler in the `ViewModel`, you will see that:\n1. A native Android `android.widget.Button` is created and added to the page. \n2. A native `java.io.File` is created. \n3. An OnClickListener interface implementation is set to the button, and inside we **call file.getName()**. \n\nSo, the `java.io.File` instance is enclosed by the native button's `onClick` callback implementation, but with `markingMode: none` enabled the framework longer takes care of finding out that connection. When GC happens in V8 or in Android the `java.io.File` instance (or its native representation) is GC'ed. This can result in either Java or JavaScript instance missing and upon calling of the `onClick` the app crashes with any of the a.m. errors.\n\nTo prevent this from happening, we should make sure the `java.io.File` instance is alive as long as we need it in the app execution. In it would be enough to live as long as the ViewModel instance is alive (we don't expect we should handle button clicks when the view is dead, right?). That is why we make it a property of the `ViewModel` class:\n\n```js\nexport class ViewModel extends Observable {\n...\n    private myFile: java.io.File;\n...\n```\n\nand use it in the callback implementation like:\n```js\nbtn.setOnClickListener(new android.view.View.OnClickListener(\n    {\n        onClick: () =\u003e {\n            this.fileName =`${this.myFile.getName()} exists at ${new Date().toTimeString()}`;\n        }\n    }\n));\n```\nThis will ensure that the GC will not collect the `java.io.File` instance unless the object that holds it (the `ViewModel` instance) is not collecteted.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnativescript%2Fmarking-mode-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnativescript%2Fmarking-mode-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnativescript%2Fmarking-mode-example/lists"}