glide icon indicating copy to clipboard operation
glide copied to clipboard

IllegalStateException: Cannot unregister not yet registered manager

Open rd7773 opened this issue 3 years ago • 2 comments

Glide version 4.13.1

Issue: App crash in RequestManager class. IllegalStateException: Cannot unregister not yet registered manager

On checking the code in RequestManager, i see in the constructor we are adding 'this' (RequestManager) as listener to the lifecycle object (ActivityFragmentLifecycle) using fun addListener. Now in addListener fun of ActivityFragmentLifecycle, we are calling onDestroy() on this listener if lifecycle is destroyed which further calls glide.unregisterRequestManager() but as we are registering request manager only after adding the listener in RequestManager constructor, it's crashing.

Screenshot 2022-05-04 at 12 04 39 PM

rd7773 avatar May 04 '22 06:05 rd7773

This is kind of broken as expected.

The best we could do is improve the assertion so that it fails with a more obvious reason. We already try to assert that you do not call Glide.with on a destroyed activity: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java#L381-L385

sjudd avatar Aug 26 '22 16:08 sjudd

We already have a check of activity.isDestroyed before calling Glide.with but still it seems like when multiple requests are submitted, till the time code execution calls RequestManager constructor, activity gets destroyed.

Can't we simply move the line glide.registerRequestManager(this) as the first thing in the RequestManager constructor?

rd7773 avatar Sep 01 '22 10:09 rd7773

This crash may happen when we call Glide.with on a destroyed fragment, because RequestManagerRetriever.get(fragment) only assert fragment.getContext() can not be null. Preconditions.checkNotNull( fragment.getContext(), "You cannot start a load on a fragment before it is attached or after it is destroyed");

fengwenhaojx avatar Oct 20 '22 06:10 fengwenhaojx

@sjudd @glide-copybara-robot To avoid this crash, can we not simply move the line glide.registerRequestManager(this) as the first thing in the RequestManager constructor? Issue is: RequestManager().constructor[line 139] > ActivityFragmentLifecycle.addListener(this)[Line 35] > RequestManager.onDestroy()[Line 378] > glide.unregisterRequestManager(this) is being called even before glide.registerRequestManager(this) is called at RequestManager().constructor[line 147]

rd7773 avatar Mar 07 '23 09:03 rd7773