kakajansh / echo

Laravel Echo for your Flutter apps.
MIT License
107 stars 68 forks source link

Potential memory leak? #22

Closed Chris1234567899 closed 2 years ago

Chris1234567899 commented 4 years ago

I get an error when I reload the widget where echo is initialized.

I have something like:

   Echo echo = new Echo({
      'broadcaster': 'pusher',
      'client': pusher,
       ...
});
...
echo.channel(
            'private-chat.' + widget.id.toString())
        .listen('ChatMessage', (e) {
      Map<String, dynamic> data = json.decode(e);
      ChatMessagemessage = ChatMessage.fromJson(data["chatMessage"]);
      print('ChatMessagereceived ' + message.toString());
      setState(() {
        _messages.insert(0, message);
      });
    });

Error log:

E/flutter ( 9754): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: setState() called after dispose(): _ChatPageState#09800(lifecycle state: defunct, not mounted)
E/flutter ( 9754): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter ( 9754): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

I guess on widget dispose we have to unsubscribe from echo or pusher (or both). Is there a method implemented to do this? echo.leaveChannel() maybe?

In case there is already, pls update the docs, so people know.

Also the docs for pusher with socket.on('connect', () => print('connect')); socket.on('disconnect', () => print('disconnect')); seem to be error prone. Maybe remove that part.

Good job by the way!

krns commented 4 years ago

Here you go: https://github.com/kakajansh/echo/blob/master/lib/laravel_echo.dart#L47-L65

kakajansh commented 2 years ago

Closed for inactivity. Feel free to reopen.