daltoniam / Starscream

Websockets in swift for iOS and OSX
Apache License 2.0
8.36k stars 1.21k forks source link

Resolve memory leak on NativeEngine #1043

Open michaelrevlis opened 1 month ago

michaelrevlis commented 1 month ago

Issue Link 🔗

Fixes #832 (Is there a way to 'clean up' socket connection to avoid memory leaks, before attempting to reconnect?) Fixes #938 (Memory leak when use native engine) Fixes #995 (if you socket resart many time,cpu is very busy and Forever and ever)

Goals âš½

According to the Apple document,

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you do not invalidate the session by calling the invalidateAndCancel() or finishTasksAndInvalidate() method, your app leaks memory until it exits.

The URLSession is retained because neither invalidateAndCancel() nor finishTasksAndInvalidate() has been called. Consequently, this leads to memory leaks after initializing a new WebSocket and establishing a connection.

Implementation Details 🚧

Invalidate the NativeEngine's URLSession at WebSocket dealloc.

Unit Test Results: Passed Screenshot 2024-10-20 at 5 28 56 PM