Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top