0

I am trying to upload image on server, the request is working perfectly but there is a problem with memory. So when I upload the image and get success there is no memory leaks but when I try to cancel this upload task memory is increasing. How can I fix this problem?

Here is the code I used to initialize NSURLSession:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.URLCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
_session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

Here is how I am trying to cancel upload task:

- (IBAction)cancelAll:(id)sender {
    [self.uploadTask cancel];
    [self.session invalidateAndCancel];
    self.uploadTask = nil;
    self.session = nil;
}

Do not think that in uploadTask is the problem, here is the code:

- (void) saveImage:(UIImage *)image onServerWithUrl:(NSURL *)url {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = [NSString stringWithFormat:@"boundary.%08x%08x", arc4random(), arc4random()];
    NSString *contentType = [@"multipart/form-data; boundary=" stringByAppendingString:boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    NSData *imageData = UIImagePNGRepresentation(image);

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"file1\"; filename=\"file1.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    self.uploadTask = [_session uploadTaskWithRequest:request fromData:body
                  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                      if (error) {
                          NSLog(@"%@", error);
                      } else if (data) {
                          NSError *err;
                          NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
                          NSLog(@"%@", dict);
                      }
                  }];
    [self.uploadTask resume];
}

EDIT As I was suggested I've used instruments so here is what I've got:

enter image description here enter image description here

Here is the code for button when it's tapped:

- (IBAction)uploadImage:(id)sender {
    __weak typeof(self) weakSelf = self;
    [self getServer:^(NSString *url) {
        [weakSelf saveImage:weakSelf.imageView.image onServerWithUrl:[NSURL URLWithString:url]];
    }];
}

I used here weakSelf why is it showing me that success block consumes about 350 mb Oo

astrolka
  • 133
  • 1
  • 11
  • open your time profiler, and check what is happening wrong. This is the best way to detect. – Gagan_iOS May 03 '17 at 09:18
  • @Gagan_iOS I've just updated the question. Really cannot understand why it is retaining some data somewhere? – astrolka May 03 '17 at 09:46
  • Maybe you need this. https://stackoverflow.com/questions/32878257/ios-memory-management-issue-with-arc – T_Wang Jun 25 '18 at 10:21

0 Answers0