0

I've been having a memory leak in a upload speedtest function that has been recently converted to ARC. I believe I've adhered to the memory management guidelines for ARC. The issue seems to be with the chuck of random data I create for the upload test. Its memory doesn't seem to get freed.
Here is where I create the upload data and ASIHTTPRequest object:

ASIHTTPRequest *request0 = [ASIHTTPRequest requestWithURL:uploadTestURL];
__weak ASIHTTPRequest *request = request0;

NSData *uploadData ;
if ([speedTier isEqualToString:@"Wifi"]) {
    uploadData  = [self createRandomNSDataOfSize:1000000];
}else
{
    uploadData  = [self createRandomNSDataOfSize:4000000];
}

[request appendPostData:uploadData];

The function that actually creates the data is:

NSMutableData* theData = [NSMutableData dataWithCapacity:size];
for( unsigned int i = 0 ; i < size/4 ; ++i )
{
    u_int32_t randomBits = arc4random();
    [theData appendBytes:(void*)&randomBits length:4];
}
return theData;

I then proceed to set up the block for setBytesSentBlock, where I manage the graphics for the upload and moment of termination of upload. Some of the code is below:

 [request0 setBytesSentBlock:^(unsigned long long size, unsigned long long total) {

    double timeDiffereceFromStart = [[NSDate date] timeIntervalSinceDate:start];

    if (totalUploadSize == 0)
    {
        start=[NSDate date];
        totalUploadSize = [request.postBody length];
        return;
    }
    if(startPosition == 0 && timeDiffereceFromStart >= 1)//[request totalBytesSent] > 20000)
    {
        startPosition = [request totalBytesSent];
        start=[NSDate date];
        return;
    }

I've just posted some of the code, but wanted to show where I used the variable 'request' within the block. I'm pretty sure I've fixed the circular retain issue here, but I wanted to make sure there wasn't some other problem.

On other thing I should note - I've put a break point within the ASIHTTPRequest dealloc function. All of the objects of this type that I create hit the dealloc breakpoint. So they are all being freed properly. But I don't understand why the memory usage keeps going up when it hits the upload function. Thanks!

  • See the accepted answer for [this question](http://stackoverflow.com/questions/8375508/why-are-my-asihttprequest-files-showing-arc-errors) – Niraj Jan 29 '14 at 17:39
  • Hi Niraj, did you mean that I should move to AFNetworking instead? Is this issue caused by the interaction between my ARC code, and ASIHTTPRequest which isn't ARC? – user3121003 Jan 29 '14 at 17:45
  • Where are you seeing the memory increase? Debug navigator or in instruments? – Putz1103 Jan 29 '14 at 17:52
  • @Putz1103, in both. In the allocations instrument, but not memory leak instrument. The allocations instrument points to setBytesSentBlock within the upload function. Also, when I reduce the size of the upload data to 400k, the increase in memory is also reduced. – user3121003 Jan 29 '14 at 17:55
  • So in Allocations instrument the "live bytes" keeps increasing when using these functions? (sorry for being stupid, I just want to be very clear on your situation). – Putz1103 Jan 29 '14 at 17:57
  • It would be nice if you could move to something like AFNEtworking, but for some reason if you can't, simply use ASIHTTPRequest as-is. If you want to use ASIHTTPRequest in your ARC-enabled project, use can disable ARC for just ASIHTTPRequest see this [link](http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project) – Niraj Jan 29 '14 at 17:57
  • Change the __weak reference to __block. And also make sure that the object `start` is also a __block attributed object. – Putz1103 Jan 29 '14 at 17:59
  • I've tried both __weak and __block. 'start' is also __block. I know that the ASIHTTPRequest objects are being deallocated (I put a break point in the dealloc function). So I don't think there is a retain cycle here. – user3121003 Jan 29 '14 at 18:12

1 Answers1

0

I've figured out the issue, and it was a retain cycle which involved the parent class of the class from which I posted the code. Because this part of the system isn't wasn't written by me, I missed it. I ended up fixing the warnings that point out retain cycles when using blocks, and the memory leaks were gone.