Skip to content

Android: Crash with "markingMode: none" + navigation + backgroundColor change (NS 5.4 / 6.0) #7867

Closed

Description

@jpierront

Environment

  • CLI: 5.4.? & 6.0.2
  • Cross-platform modules: 5.4.2 & 6.0.1
  • Android Runtime: 5.4.0 & 6.0.0
  • NativeScript-Angular: 8.0.0 & 8.0.2
  • Angular: 8.0.0 & 8.0.3

Tested on Android Emulator 8.1

Describe the bug
I get "Attempt to use cleared object reference" errors in a few seconds with the MarkingMode = none. (Again: #7125)

You have to change background colors of elements while navigating to a new route several times. After a while it crash.

Use case: Change background color of current route in a menu.

  • It don’t crash when you only navigating
  • It don’t crash when you only change background color
  • It crash when you do both
  • It don’t crash when you change text color instead of background color
  • It crash quicker when you do lot of calculations in the app

To Reproduce

  • Navigate to a new route every second (router.navigate)
  • Change Background color of current route in the menu ([class.active]=« … »)
  • Overloading the app with other changes (Here other background color changes) (Necessary to accelerate the crash)

Expected behavior
No crash.

Sample project
https://play.nativescript.org/?template=play-ng&id=4RKa1j&v=2
Download it, add "markingMode": "none" in app/package.json under « android » object
Yes, it don’t crash directly in PlayGround.

Additional context
Observed on my application and reproduced in a blank application.
In my app it can crash after 3 or 40 route changes.

Activity

  1. valera1401 commented on Jul 27, 2019

    @valera1401
  2. assigned and unassigned on Jul 29, 2019
  3. devlfm commented on Jul 31, 2019

    @devlfm

    I guess this is related #4204 too, and goes way back in time.. We still have no solution for this.

  4. gkoulin commented on Sep 23, 2019

    @gkoulin

    Any news about this issue? I'm getting regular crashes too

  5. changed the title [-]Android: Crash with "markingMode: none" ... Again (NS 5.4 / 6.0)[/-] [+]Android: Crash with "markingMode: none" + navigation + backgroundColor change (NS 5.4 / 6.0)[/+] on Sep 30, 2019
  6. Skyless7 commented on Oct 7, 2019

    @Skyless7

    I was getting regular crashes aswell, excatly like described in the ticket. Nativescript's Sidekick has template with this issue, if you chose while creating a new prooject a project with sidedrawer, it has this bug in the app.component "[class.selected]="isComponentSelected('/sample')"

    Then when navigating between different pages in the sidedrawer, it often pops out that error while app crashes. Sometimes with error reported, soometimes without error.

    When this class.selected is removed, app works flawlessly

  7. Yermo commented on Oct 12, 2019

    @Yermo

    May be related to this: #7954

  8. 9 remaining items

  9. flocca commented on Nov 5, 2019

    @flocca

    In 6.2 the workaround suggested by @jpierront needs to be updated:

    platform !== "android" ? {} : {
      test: /@nativescript\/core\/ui\/styling\/background\.android\.js$/,
      use: [{
        loader: 'string-replace-loader',
        options: {
          search: 'defaultDrawable = cachedDrawable.newDrawable(nativeView.getResources());',
          replace: 'defaultDrawable = null;',
          strict: true,
        }
      }]
    },
    
  10. jpierront commented on Nov 26, 2019

    @jpierront
    Author

    @sandwai-scottjackman npm install --save-dev string-replace-loader ?

  11. ivandejesus commented on Nov 26, 2019

    @ivandejesus

    Hi @jpierront,

    having error when adding the script

    ERROR in ../node_modules/tns-core-modules/ui/styling/background.js
    Module build failed (from ../node_modules/string-replace-loader/index.js):
    Error: Replace failed (strict mode) : defaultDrawable = cachedDrawable.newDrawable(nativeView.getResources()); → defaultDrawable = null;
    at replace (/home/ivandejesus/Desktop/ams-pwa-native/node_modules/string-replace-loader/lib/replace.js:17:11)
    at Object.processChunk (/home/ivandejesus/Desktop/ams-pwa-native/node_modules/string-replace-loader/lib/processChunk.js:11:17)
    @ ../node_modules/@nativescript/core/ui/styling/style-properties.js 6:19-57
    @ ../node_modules/@nativescript/core/ui/styling/css-animation-parser.js
    @ ../node_modules/@nativescript/core/ui/styling/style-scope.js
    @ ../node_modules/tns-core-modules/ui/styling/style-scope.js
    @ ../node_modules/nativescript-dev-webpack/load-application-css.js
    @ ../node_modules/nativescript-dev-webpack/load-application-css-angular.js
    @ ./main.ts

  12. jpierront commented on Nov 26, 2019

    @jpierront
    Author

    @ivandejesus The replacement is "strict" (required) to be sure that the fix is applied.
    Since 6.2 "tns-core-modules" package can be replaced by "@nativescript/core" package.
    See #7867 (comment)

  13. j99ht commented on Dec 27, 2019

    @j99ht

    I do not see background.android.js in [email protected]
    Any idea on how to implement the workaround for this release?

  14. leocrawf commented on Jan 7, 2020

    @leocrawf

    Nativescript 6.3 now started using @nativescript/core so its now: node_modules/@nativescript/core/ui/styling/background.android.js

  15. ebewley commented on Mar 28, 2020

    @ebewley

    I am using NativeScript v6.4.1 and Angular v8.3.25 and have found that after removing the [class.-selected]="isComponentSelected('/_some_route_')" lines from RadSideDrawer GridLayout elements, all crashes in my app suddenly went away, including those referencing Drawable. Might be a good place to start in eliminating them from your app as well, until the underlying bugs can be corrected.

    For example, in my app.component.tns.html file, I removed the [class.-selected] attribute from all elements, such as to change the following GridLayout element...

    <GridLayout columns="auto, *" class="nt-drawer__list-item" (tap)="onNavItemTap('/home')"
            **[class.-selected]="isComponentSelected('/home')"**>
        <Label col="0" text="&#xf015;" class="nt-icon fas"></Label>
        <Label col="1" text="Home" class="p-r-10"></Label>
    </GridLayout>
    

    ...to the following:

    <GridLayout columns="auto, *" class="nt-drawer__list-item" (tap)="onNavItemTap('/home')">
        <Label col="0" text="&#xf015;" class="nt-icon fas"></Label>
        <Label col="1" text="Home" class="p-r-10"></Label>
    </GridLayout>
    
    
  16. arturikoX commented on Mar 29, 2020

    @arturikoX

    @ebewley,
    You are right ! removing the dynamic classes works for me too...
    I changed a little bit isComponentSelected and no crashes too ( I hope.. ) :

        import * as application from '@nativescript/core/application';
    
    ...
        isComponentSelected (url: string): boolean {
            const sideDrawer = <RadSideDrawer>application.getRootView();
            if (!sideDrawer || !sideDrawer.getIsOpen()) {
                return;
            }
            return this._activatedUrl === url;
        }
    
  17. srokatonie commented on Apr 13, 2020

    @srokatonie

    @ebewley,
    You are right ! removing the dynamic classes works for me too...
    I changed a little bit isComponentSelected and no crashes too ( I hope.. ) :

        import * as application from '@nativescript/core/application';
    
    ...
        isComponentSelected (url: string): boolean {
            const sideDrawer = <RadSideDrawer>application.getRootView();
            if (!sideDrawer || !sideDrawer.getIsOpen()) {
                return;
            }
            return this._activatedUrl === url;
        }
    

    @arturikoX @ebewley I left the dynamic class and just used rewritten isComponentSelected method and not issues so far (Android Emulator Pixel 9).

  18. jpierront commented on Apr 13, 2020

    @jpierront
    Author

    Any background color changes in any app may randomly cause a crash. As mentioned above, this hack fixes the problem:

    Add this in webpack.config.js just before "return config'

    if (platform === 'android') {
      config.module.rules.unshift({
        test: /@nativescript\/core\/ui\/styling\/background\.js$/,
        use: [{
          loader: 'string-replace-loader',
          options: {
            search: 'defaultDrawable = cachedDrawable.newDrawable(nativeView.getResources());',
            replace: 'defaultDrawable = null;',
            strict: true,
          },
        }],
      });
    }
    

    Maybe one day the NativeScript team will find a fix ...

  19. vahidvdn commented on Apr 15, 2020

    @vahidvdn

    @jpierront It seems that this is fixed as mentioned here.

  20. ibaraness commented on Aug 1, 2020

    @ibaraness

    Hey, I don't know if it's related to general discussion. But if it can help anyone, I had similar problem where my app would crush sometimes when using *ngFor for custom navigation bar with [ngClass] or [class.selected] on children (Only with markingMode: none):

    <ng-container  *ngFor="let item of topItems">
        <Label  [text]="item" [class.selected]="item === selectedItem" (tap)="selectTopItem(item)"></Label>
    </ng-container>
    

    Would give me: "Attempt to use cleared object reference id=..." error

    So, I've wrapped the inside ng component and replaced the the [class.selected] with *ngIf directive, which did the work. It's a bit ugly, but now it's not crushes:

    <ng-container  *ngFor="let item of topItems">
        <my-component  [item]="item" [selectedItem]="selectedItem" (selectItem)="selectTopItem($event)"></Label>
    </ng-container>
    
    // Component template
          <Label *ngIf="item === selectedItem; else notSelected" class="selected top-item" [text]="item" 
                   (tap)="selectTopItem.emit(item)"></Label>
            <ng-template #notSelected>
                <Label class=" top-item" [text]="item" (tap)="selectTopItem.emit(item)"></Label>
            </ng-template>
    

    By the way, I couldn't do it outside of the component, because it would mess the order when selected item changes

  21. NathanWalker commented on Oct 23, 2026

    @NathanWalker
    Contributor

    The background-drawable caching that caused these "Attempt to use cleared object reference" crashes was fixed in #8475, and markingMode: none is now the default. Closing as completed. Please open a new issue with a repro if you still hit it on current @nativescript/core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions