I am creating a library for HTTPS access using NSURLSession. My library is built with non-ARC.
I'm trying to create a class like this:
@interface MyURLSession : NSObject <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
:
@property (nonatomic, assign) MyInputStream *inputStream;
:
@end
For the GET Method, I created a task with dataTaskWithRequest and was able to access it without memory leaks.
As for the POST Method, when I created and processed the task with uploadTaskWithStreamedRequest, many memory leaks was detected in Leaks of Instruments.
Specifically, calling completionHandler(_inputStream) in URLSession:task:needNewBodyStream: leaks. I think that many other leaks are caused by passing the _inputStream to the completionHandler.
Searching Leaks for HTTP, Session, and Stream words looks like this:
Snapshot Growth Persistent
MyInputStream 176 Bytes 1
CFHTTPMessage 1.72 KiB 6
HTTP2Connection 1.06 KiB 2
HTTP2Stream 928 Bytes 2
HTTPConnectionCacheKey 384 Bytes 3
HTTP2ConnectionCache 288 Bytes 2
HTTP2ConnectionCacheEntry 256 Bytes 2
HTTPHeaderDict 192 Bytes 6
std::__1::__shared_ptr_pointer<HTTP2Stream*, std::__1::default_delete<HTTP2Stream>, std::__1::allocator<HTTP2Stream> > 64 Bytes 2
__NSCFURLSessionConfiguration 512 Bytes 1
__NSURLSessionLocal 368 Bytes 1
HTTP2Stream 928 Bytes 2
RequestBodyStreamProvider 352 Bytes 2
_CFStreamDelegate 128 Bytes 2
std::__1::__shared_ptr_pointer<HTTP2Stream*, std::__1::default_delete<HTTP2Stream>, std::__1::allocator<HTTP2Stream> > 64 Bytes 2
std::__1::__shared_ptr_pointer<__CFReadStream*, Deleter_CFRelease, std::__1::allocator<__CFReadStream> > 64 Bytes 2
std::__1::__shared_ptr_pointer<BlockHolderVar<CFStreamError>*, SmartBlockWithArgs<CFStreamError>::Deleter, std::__1::allocator<BlockHolderVar<CFStreamError> > > 64 Bytes 2
I also searched Leaks for words such as Task and Request, but couldn't find them.
Since the reference counter of _inputSteam is 1 in the end, it seems that the one passed by completionHandler is still referenced.
I tried releasing more reference counters once, and although _inputSteam was released, the other leaks weren't resolved.
Passing MyInputStream * inputSteam is a original derivative class of NSInputStream and is a simple class that just passes data using read:maxLength:.
Could you give me some advice on how to resolve those leaks?
Thanks.
My Environments:XCode 12.4, dev target:iOS 7.0+, Device:iPhone X(OS 12.1)