-
-
Notifications
You must be signed in to change notification settings - Fork 180
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
Dispose in reverse order #81
Comments
Ah, indeed. I would swear I reversed it. I remember fixing the failing tests and everything... Thanks, will do. |
We’ve just encountered the same problem. Can you please merge the fix? |
It's not that simple. While such change fixes a few things, it also breaks others. Similarly, React doesn't dispose of them in reverse order. And clearly, they use hooks on a much larger scale |
Could you merge it and just give the library a flag to define forward/reverse order. |
@derolf You mentioned that this caused a lot of issues. Could you clarify what these are? For now, I'm still not convinced if we should do something about it or not. |
Generally, I think reverse order is natural since that is what you usually do in dispose/destructors: you dispose stuff in the reverse order of creation. That is especially important if there are dependencies among the things you create. Concrete: I have a bunch of hooks to create scrollcontrollers, texteditingcontrollers etc. and then in a large fraction I am using useListenable to connect to them. During destruction, the controllers are killed first and then ListenableHook tries to remove itself which crashes the app. |
The issue is, reverse order cause problems too because hooks have a "keys" that allow them to be disposed of on command. So for example, we can have: final controller = useMemoized(() => MyController(someKey), [someKey]);
useMyController(controller); In that situation, we're stuck. Because changing So having hooks disposed only from top to bottom allows some form of consistency. |
Similarly, React doesn't dispose hooks in reverse order of creation, and their hooks are used on a much larger scale. A potential solution may be to offer 3 kind of hooks instead of two (or at least 2 + a flag):
Although this may not solve more advanced use-cases with animations and tweens and stuff. |
In that case either order will be wrong since when Probably we need a solution that allows to "scope" hooks inside other hooks. |
That's also what I am thinking of. It would be nice if all |
The issue being, React doesn't have this problem, because they never use I tried to speak a bit with Dan Abramov on that topic some months ago, but got no concrete solution out of it. |
What do you propose to fix the following: final queryTextController = useTextEditingController();
useEffect(() {
final cbOnChanged = () { // do something usefull };
queryTextController.addListener(cbOnChanged);
return () => queryTextController.removeListener(cbOnChanged);
}); This throws:
Should I just drop the removeListener? :D |
I created our own variant of
|
Interesting fix! I'm not too good with GC, would it potentially create memory leaks (And work with streams)? |
@derolf i am running into the same issue. Are you still using the custom hook, or do you guys have something new? |
As a comeback to that:
Maybe one possibility is to delay the disposal of hooks in that situation to the end of the frame. By doing so, we would have the list of all the hooks that need to be disposed of. So we could do it in reverse order. |
After all this time, I've finally decided to reverse the dispose order. |
Sounds good, but why finally? |
No particular reason. I was always doing something else instead.
…On Sat, Jun 20, 2020, 14:25 derolf ***@***.***> wrote:
Sounds good, but why finally?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#81 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEZ3I3OWCFEZ33HIXXHUJETRXS2FVANCNFSM4HK4VQAQ>
.
|
@rrousselGit I recall there were some limitations with an original draft you created. Is that solved? |
Yes. I overestimated the consequences at that time. The only situation where this could be surprising is, it may create some exceptions after a hot-reload that reorder hooks. |
Hi, today I updated from 0.9.0 to 0.14.1 and I received Is there a way to fix this? I use this code: final scrollController = useMemoized(() => ScrollController());
useEffect(() {
scrollController.addListener(onScroll);
return () => scrollController.removeListener(onScroll);
}, [key]);
useEffect(() {
return () => scrollController.dispose();
}, []); |
Move your first useEffect after the second |
@andrea689 you are calling the final scrollController = useMemoized(() => ScrollController());
useEffect(() {
return () => scrollController.dispose();
}, []);
useEffect(() {
scrollController.addListener(onScroll);
return () => scrollController.removeListener(onScroll);
}, [key]);
|
Thank you to all, it is working! Sorry but I'm new on hooks. |
I am trying to rebuild my
Doing that results in and error:
|
Hello, I am having a similar problem with useState to show a progress dialog hud. #this is my code init
#this is my state change
it works fine, unless I leave the screen while loading, in this case the state is disposed but code keeps running and try to set it anyway. Would be nice if I could check whether showDialog is disposed before update the value (I wouldn't need to change value if its disposed already). Is there some workaround or fix? Thanks |
@brunodmn You can trye the |
It worked this way, thank you! I'd tried with bellow my updated snippets, for reference: init...
state change
|
Currently, flutter_hooks disposes hooks from the top down, which is strange considering that Flutter itself disposes states from the bottom up. This causes exceptions when removing ChangeNotifier listeners that get cleaned up by useEffect's disposal callback.
The text was updated successfully, but these errors were encountered: