Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2] Tried to create view after it has already been destroyed #3767

Closed
pibo opened this issue Aug 14, 2018 · 61 comments
Closed

[v2] Tried to create view after it has already been destroyed #3767

pibo opened this issue Aug 14, 2018 · 61 comments

Comments

@pibo
Copy link

pibo commented Aug 14, 2018

My app users are complaining about random crashes on Android. I have Crashlytics installed and I verified by using user and time filters that this crashes are caused by the following error:

captura de tela 2018-08-14 as 18 17 04

I can't reproduce the problem in the Android simulator and it seems Android exclusive, as it appears. Can anyone help me what might be happening?
Thanks in advance.


Environment

  • React Native Navigation version: 2.0.2377
  • React Native version: 0.55.4
  • Platform(s) (iOS, Android, or both?): Android
  • Device info (Simulator/Device? OS version? Debug/Release?): Only device, apparently
@nezaidu
Copy link

nezaidu commented Aug 17, 2018

same

@nezaidu
Copy link

nezaidu commented Aug 17, 2018

somebody pls remember me to show you how to reproduce, i know how, very busy right now, might forget

@starzle
Copy link

starzle commented Aug 26, 2018

See the same crash in Android only. In my case, it happens when a child screen is popping (the pop animation is running) when the setRoot() method is called. If I set the pop animation's duration to 0, it works without any problem. So I think the pop animation is the culprit here.

Hope this information can help you resolve the issue.

@guyca
Copy link
Collaborator

guyca commented Aug 27, 2018

Unfortunately I can't fix this one without a reproduction, @nezaidu ?

@reganperkins
Copy link

getting the same thing but no consistent way of reproducing this.

@guyca
Copy link
Collaborator

guyca commented Aug 29, 2018

@starzle Have you tried awaiting the pop command so root is set after pop completes?

@starzle
Copy link

starzle commented Aug 29, 2018

@guyca In our case, the setRoot is called when componentDidAppear is triggered. And apparently, componentDidAppear is triggered before the pop animation ends. So we don't have a place where we can await the pop.

But if we simply add a delay to the setRoot call, then as long as the delay is long enough to make sure pop animation is already finish (onAnimationEnd called if I am correct) when setRoot is called, then the crash goes away. In short, pop's onAnimationEnd needs to be called before setRoot.
screen shot 2018-08-29 at 11 56 46 pm

Hope this info can help you.

@suetming
Copy link

suetming commented Sep 1, 2018

after add code push relative,
code push update restart app, android app will crash,
logcat console out put this log

java.lang.RuntimeException: Tried to create view after it has already been destroyed

@zirtuedeveloper
Copy link

i have same issue

@zirtuedeveloper
Copy link

componentDidMount(){


 
  this._getDeviceToken();

  

  FCM.getInitialNotification().then(notif => {
    console.log(notif,'notif tapped')
    if (notif.gotoActivity==="dashboard") {
      startMainTab(2);
      return;
    }
    else if (notif.gotoActivity==="activity") {
      startMainTab(0);
      return;
    }

  
  });

  
  FCM.on(FCMEvent.Notification, this.handleNotification);


  this.tokenToState();

  
  

}

@reganperkins
Copy link

reganperkins commented Sep 19, 2018

this also occurs when you have a root view, navigate to a new view with pushTo from a modal and then try to setStackRoot within the main application.

@haggholm
Copy link

To add to my colleague @reganperkins above, our error is the same but the stacktrace does not seem entirely identical.

09-19 14:13:26.303 5362-5362/co.elk.rn D/ERROR: java.lang.RuntimeException: Tried to create view after it has already been destroyed
        at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:153)
        at com.reactnativenavigation.viewcontrollers.ViewController.lambda$runOnPreDraw$2$ViewController(ViewController.java:266)
        at com.reactnativenavigation.viewcontrollers.ViewController$$Lambda$2.run(Unknown Source)
        at com.reactnativenavigation.utils.UiUtils$1.onPreDraw(UiUtils.java:27)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

We managed to find a workaround that may (or may not!) clarify the situation. As she was saying, the control flow in our scenario is this:

  1. Start on a root view.
  2. Open a modal (as it happens, to create a new item).
  3. From the modal, navigate via pushTo() to a new view (the new item).
  4. From the new item, navigate via setStackRoot() to the root component. (Of course, in this specific scenario, we could pop, but the code is more generic.)

The workaround: Close the modal before pushing the new view, deferring the push using setImmediate(). Now, for some reason, it no longer crashes. Perhaps the open modal does something strange to the stack?

@DaneEveritt
Copy link

I can confirm this happening as well. In my case I'm calling a setRoot on a login screen to bump an already logged in user into the app.

Heres the stack from the crash, only affects android devices.

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.lawnlove.provider, PID: 17401
                  java.lang.RuntimeException: Tried to create view after it has already been destroyed
                      at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:152)
                      at com.reactnativenavigation.viewcontrollers.ParentController.getView(ParentController.java:63)
                      at com.reactnativenavigation.viewcontrollers.stack.StackController.lambda$push$4$StackController(StackController.java:157)
                      at com.reactnativenavigation.viewcontrollers.stack.StackController$$Lambda$4.run(Unknown Source:0)
                      at com.reactnativenavigation.anim.NavigationAnimator$1.onAnimationEnd(NavigationAnimator.java:64)
                      at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
                      at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1293)
                      at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
                      at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
                      at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
                      at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                      at android.view.Choreographer.doFrame(Choreographer.java:655)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6944)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
E/UncaughtException: java.lang.RuntimeException: Tried to create view after it has already been destroyed
                         at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:152)
                         at com.reactnativenavigation.viewcontrollers.ParentController.getView(ParentController.java:63)
                         at com.reactnativenavigation.viewcontrollers.stack.StackController.lambda$push$4$StackController(StackController.java:157)
                         at com.reactnativenavigation.viewcontrollers.stack.StackController$$Lambda$4.run(Unknown Source:0)
                         at com.reactnativenavigation.anim.NavigationAnimator$1.onAnimationEnd(NavigationAnimator.java:64)
                         at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
                         at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1293)
                         at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
                         at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
                         at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
                         at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
                         at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                         at android.view.Choreographer.doFrame(Choreographer.java:655)
                         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                         at android.os.Handler.handleCallback(Handler.java:789)
                         at android.os.Handler.dispatchMessage(Handler.java:98)
                         at android.os.Looper.loop(Looper.java:164)
                         at android.app.ActivityThread.main(ActivityThread.java:6944)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

@zsajjad
Copy link

zsajjad commented Nov 10, 2018

Any update on this @guyca ?

@makirby
Copy link

makirby commented Nov 12, 2018

Wrapping the setStackroot calls with InteractionManager.runAfterInteractions, solved this for me.

@zsajjad
Copy link

zsajjad commented Nov 12, 2018

@makirby can you please share a snippet for the solution?

@makirby
Copy link

makirby commented Nov 12, 2018

Basically where we were making a setStackRoot call soon after another push in a modal stack, I would do something like this.

 InteractionManager.runAfterInteractions(() => {
                    Navigation.setStackRoot(STACK_IDS.main, {
                      component: { name: SCREEN_MAP.Home },
                    })
                  })

@stale
Copy link

stale bot commented Dec 12, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

@stale stale bot added the 🏚 stale label Dec 12, 2018
@dozoisch
Copy link
Contributor

probably not stale

@stale stale bot removed the 🏚 stale label Dec 12, 2018
@kyle-ssg
Copy link
Contributor

kyle-ssg commented Mar 28, 2019

Can confirm I've just started facing this, thanks for the catch @zabojad 👍. This seems to be Android only.

Edit: This was actually my own fault (although the hard crash without indication wasn't ideal). This is also replicable on android if you try to push to a screen twice from the same component id. iOS side silently deals with it.

@bahmannejati
Copy link

bahmannejati commented Mar 30, 2019

Here is my code for reproduce this issue & also found tricks to solve the issue :

for(let i = 0; i < 20; i++){
	Navigation.setRoot({
		root: {
			component: {
				id: SCREENS.HUB,
				name: SCREENS.HUB,

				passProps: {
				}
			}
		}
	});
	Navigation.setRoot({
		root: {
			sideMenu: {
				center: {
					stack: {
						children: [
							{
								component: {
									id: SCREENS.HOME,
									name: SCREENS.HOME,
								}
							}
						],
					}
				},
				right: {
					component: {
						id: SCREENS.SIDE_MENU,
						name: SCREENS.SIDE_MENU,
					}
				}
			}
		}
	});
}

I placed 20 in for() to make sure that error happens, because this issue doesn't happen always.
After about 3 days and testing many solutions (that no one worked correctly, even i made a task queue system for managing Navigation methods priority in a good way) i found that cant fix that permanently.

But we have two choice to get rid out of that:

1. Making app layout structure with just 1 Navigation.setRoot() and then use .push() .pop() .popToRoot() to manage navigation, it should work but still is risky because i faced Tried to create... even in my first Navigation.setRoot()

2. Simply comment (or delete :) bellow line in ViewController.java:

if (isDestroyed) {
      throw new RuntimeException("Tried to create view after it has already been destroyed");
}

This worked for me without any problem, that is a quick TEMPORARY solution, not PERMANENTLY.

(ViewController.java is here : /node_modules/react-native-navigation/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/)

@Zaporozhec7
Copy link

If I try use method 2. mentioned by @bahmannejati , I have segfault instead that exception

@nilocoelhojunior
Copy link

Sorry @varungupta85 for delay. In my case, I have two navigations one Stack and one with BotoomTabs. And to manage this navigation I have a Component with Redux. What happened was that this component was updated more than once by Redux and with that, the setRoot was also called and the app crashed.
The solution was to create an attribute in the state of the Component to control when to call the setRoot.

@fodjan-philip
Copy link

My app consists of BottomTabs containing stacks of screens. Whenever the BottomTab is changed, I call Navigation.popToRoot() for the tab that the user is leaving.

I can reproduce the crash by pushing a screen to the stack of a tab (with waitForRender in push animation) and then quickly changing to another tab and back.

It sounds like #4842 addressed that issue, but I am running v2.16.0 and still get the error.

@guyca
Copy link
Collaborator

guyca commented Apr 18, 2019

@fliPmitKlammern Could you please push a branch with a reproduction in the playground app?

@RuairiOliver
Copy link

Got this today, going with commenting it out for now as suggested earlier

@AugustoAleGon
Copy link

This issue is still in Android. I got the same error but different logs tho:


Fatal Exception: java.lang.RuntimeException
Tried to create view after it has already been destroyed
com.reactnativenavigation.viewcontrollers.ViewController.getView (ViewController.java:165)
com.reactnativenavigation.viewcontrollers.ChildController.onViewBroughtToFront (ChildController.java:45)
com.reactnativenavigation.viewcontrollers.ChildControllersRegistry.onViewDisappear (ChildControllersRegistry.java:15)
com.reactnativenavigation.viewcontrollers.ChildController.onViewDisappear (ChildController.java:41)
com.reactnativenavigation.viewcontrollers.ComponentViewController.onViewDisappear (ComponentViewController.java:48)
com.reactnativenavigation.viewcontrollers.ViewController.destroy (ViewController.java:235)
com.reactnativenavigation.viewcontrollers.ChildController.destroy (ChildController.java:70)
com.reactnativenavigation.viewcontrollers.ComponentViewController.destroy (ComponentViewController.java:93)
com.reactnativenavigation.viewcontrollers.ParentController.destroy (ParentController.java:112)
com.reactnativenavigation.viewcontrollers.stack.StackController.destroy (StackController.java:138)
com.reactnativenavigation.viewcontrollers.navigator.Navigator.destroyRoot (Navigator.java:121)
com.reactnativenavigation.viewcontrollers.navigator.Navigator.destroyViews (Navigator.java:117)
com.reactnativenavigation.NavigationActivity.onReload (NavigationActivity.java:124)
com.reactnativenavigation.react.JsDevReloadHandler$$Lambda$1.run (Unknown Source:2)

@turbix
Copy link

turbix commented May 19, 2019

The issue still reproduces on Android: after app start press back button and start app again -- crash occurs with same stack.

@stale
Copy link

stale bot commented Dec 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

@stale stale bot added the 🏚 stale label Dec 21, 2019
@stale
Copy link

stale bot commented Dec 28, 2019

The issue has been closed for inactivity.

@stale stale bot closed this as completed Dec 28, 2019
vshkl pushed a commit to vshkl/react-native-navigation that referenced this issue Feb 5, 2020
When popToRoot or popTo where called while a screen was pushed, at the end of the push animation RNN tried to destroy an already destroyed ViewController.
Related to wix#3767
bweissbart added a commit to bweissbart/flagship that referenced this issue May 7, 2020
This PR includes a series of fixes that attempt to alleviate crashing when restarting the bundle on Android.

- Use BB fork of react-native-restart package which contains a fix to the Android code
- Wrap Navigation.setRoot() in fsapp inside runAfterInteractions which appears to help alleviate Navigation being clobbered on startup (inspired by wix/react-native-navigation#3767 (comment))
- Refactor tab logic in fsapp so that tabs are generated before setting them in the Navigator
- Update tab schema to follow recommended format (wix/react-native-navigation#5096 (comment))
- Removed non-printable character from PirateShip initialization config
- Removed setStackRoot() calls in PirateShip that were being used when authentication failed on app startup. These appeared to be clobbering Navigation when it was trying to initialize the app.
@gigby
Copy link

gigby commented Jun 16, 2020

The issue still reproduces on Android: after app start press back button and start app again -- crash occurs with same stack.

This bug is still here (version 6.7.5)

@avonipenti
Copy link

Upgraded to fix the crashes but I still see the crashes in firebase.

@marf
Copy link

marf commented Jan 20, 2021

We still see the crash with version 7.8.4

@dooleyb1
Copy link

Still seeing crashes occur in Crashlytics on app using "react-native-navigation": "7.9.0"

@dooleyb1
Copy link

Any updates on this?

@parth-koshta
Copy link

This issue still exists in 7.27.1

@TrustDec
Copy link

This issue still exists in 7.27.1、7.28.1、7.29.0

crherman7 pushed a commit to crherman7/flagship that referenced this issue Nov 14, 2022
This PR includes a series of fixes that attempt to alleviate crashing when restarting the bundle on
Android.

- Use BB fork of react-native-restart package which contains a fix to the Android code
- Wrap Navigation.setRoot() in fsapp inside runAfterInteractions which appears to help alleviate
  Navigation being clobbered on startup (inspired by
wix/react-native-navigation#3767 (comment))
- Refactor tab logic in fsapp so that tabs are generated before setting them in the Navigator
- Update tab schema to follow recommended format
  (wix/react-native-navigation#5096 (comment))
- Removed non-printable character from PirateShip initialization config
- Removed setStackRoot() calls in PirateShip that were being used when authentication failed on app
  startup. These appeared to be clobbering Navigation when it was trying to initialize the app.

brandingbrand-source-id: 42d0e5126eb8c5594ca4bf6c17b95106f487e4fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests