I've noticed that the resources attached to a network request are not freed correctly.
I took the demo ‘ti create’ app.js and extended it to make a web request based on the example found at:
https://wiki.appcelerator.org/display/guides2/HTTPClient+and+the+Request+Lifecycle
You can see after 15mins the memory is still associated with these requests, which should have given GC plenty of time to collect. (instruments.png)
If we look at the memory graph associated with these objects we can see that the reference at first is held by both the JS stack via TINetworkNettpClientProxy and the NSURLSession (memorygraph1.png)
After a while the GC runs and the JS side of things is correctly freed up just leaving an instance linked to the NSURLSession. (memorygraph2.png) and from then on never gets freed.
From Apples docs:
{quote}If you no longer need a session, you can invalidate it by calling either invalidateAndCancel() (to cancel outstanding tasks) or finishTasksAndInvalidate() (to allow outstanding tasks to finish before invalidating the object). If you don’t invalidate the session, it automatically goes away when your app is terminated (unless it’s a background session with active tasks). After invalidating the session, when all outstanding tasks have been canceled or have finished, the session calls the delegate’s urlSession(_:didBecomeInvalidWithError:) method. When that delegate method returns, the session disposes of its strong reference to the delegate.{quote}
https://developer.apple.com/documentation/foundation/urlsession
Looking through the source for APSHTTPRequest I think I can see 2 issues:
* The NSURLSession that is created is never “finished” allowing for a memory leak
* The NSURLSession delegate method “didBecomeInvalidWithError” is treated as purely an error state, but instead should be considered as a success unless the error object is present
No comments