RockandRoll
RockandRoll

Reputation: 411

NSThread Causing memory Leaks in iPhone

I am uploading images chunk wise, in a background thread, each chunk will be size of 512kb,to the best of my knowledge,i have taken care of memory leaks using release,nsautoreleasepool. Below is the code for uploading images chunkwise.

- (void)FetchDataFromDB : (NSNumber *) isOffline
{
 @autoreleasepool {
    @try {
    NSLog(@"FetchDatafromDB");
    isThreadStarted = YES;

    VYukaDBFunctions *vdb = [VYukaDBFunctions getInstance];

    NSMutableArray *fileNames = [vdb GetFileNames:[isOffline integerValue]];
    for(int j=0 ; j<[fileNames count] ; j++)
    {

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString * filename = fileNames [j] ;
        int _outgoingMsgId = [[vdb SelectMsgId:filename] intValue];
        int  _totalchunk =[[vdb SelectTotalChunk:filename]intValue];
        int currentChunk = [vdb GetCurrentChunk:filename];
    for( int i=currentChunk ; i <= _totalchunk ; i++)
    {

        NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

        NSString *AsyncRequest = [[NSString alloc] init];
        AsyncRequest = [vdb SelectAsyncRequest: i : _outgoingMsgId];

        if(![AsyncRequest isEqual:@""])
        {

        BOOL status = [self UploadChunkWise :AsyncRequest : 1 : i : vdb : filename : _outgoingMsgId];
      // AsyncRequest = NULL;
     //  [AsyncRequest release];
        if(status){
            if(i==_totalchunk)
            {
                NSLog(@"Deleting from medialist , FileName :%@", filename);
                [vdb DeleteFromMediaList : filename];

            }
        }
        else{

            [vdb DeleteFromMediaList : filename];
            break;
        }
        }

        [innerPool drain];
    }

        [pool drain];
    }

    [fileNames removeAllObjects];

   // [fileNames release];

    //recurssive call to check any pending uploads..
    if([[vdb GetFileNames:[isOffline integerValue]] count] > 0)
    {
        NSLog(@"Calling Recursively..");
        [self FetchDataFromDB:[isOffline integerValue]];
    }

    }
    @catch (NSException *exception) {

        NSLog(@"Exception caught on Uploading from FetchDataFromDB:%@", exception);

    }
    @finally {

    }
}

 NSLog(@"thread quit ");
 isThreadStarted = NO;
[NSThread exit];

}

-(BOOL) UploadChunkWise :(NSString *) AsyncRequest : (int) count : (int)currentChunk : (VYukaDBFunctions * ) vdb : (NSString *) currentFileName : (int) outgoingMsgId
{

NSHTTPURLResponse *response ;
NSError *error;
//Yes, http
NSMutableURLRequest *httpRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL     URLWithString:@"Url goes here"]];
NSData* data = [AsyncRequest dataUsingEncoding:NSUTF8StringEncoding];

[httpRequest setHTTPMethod:@"POST"];
[httpRequest setHTTPBody:data];

[httpRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
NSData *returnedData = [NSURLConnection sendSynchronousRequest: httpRequest returningResponse:&response error:&error] ;
NSString *result= [[NSString alloc] initWithData:returnedData encoding:NSASCIIStringEncoding];
[httpRequest release];

returnedData= NULL;
[returnedData release];
data = NULL;
[data release];

if ([result rangeOfString:@"success"].location != NSNotFound )
{
    NSLog(@" success");
    [vdb DeleteCurrentChunkFromOutgoingTable:currentChunk : outgoingMsgId];
    [result release];
    return YES ;

}
else if ([result rangeOfString:@"fail"].location != NSNotFound )
{
    [result release];
    if (count < 3) {

        return  [self UploadChunkWise :AsyncRequest : count+1 : currentChunk: vdb : currentFileName : outgoingMsgId ];
    }
    else
    {
        NSLog(@" failed");
        [vdb DeleteAllCurrentFileChunksFromOutgoingTable:currentFileName];
        return NO ;
    }

}

return NO;

}

I am starting thread as below

 [NSThread detachNewThreadSelector:@selector(FetchDataFromDB:) toTarget:self withObject:[NSNumber numberWithInt:0]];

The problem is after uploading 9 to 12 chunks, i am getting memory error. i am getting 4 to 5 times memory warning and after that app crashes.in console i am getting memory warning first at app delegate class, followed by 4 classes which are extending UIViewController. why i am getting warning at app delegate, and other classes which is of type UIViewController.Why i have to release object of other class if the separate thread is giving me memory error? what i am doing wrong here? I cannot use ARC, as i have integrated this with old code, which is not using ARC, i tried enabling ARC class wise, but it dint work. Can any one help me to find out if there is any memory leaks in this code. Suggestions are welcomed and appreciated.Thanks in advance..

Upvotes: 0

Views: 725

Answers (1)

Daddy
Daddy

Reputation: 9035

Two things- first, I see this:

NSString *AsyncRequest = [[NSString alloc] init];
AsyncRequest = [vdb SelectAsyncRequest: i : _outgoingMsgId];

This should be consolidated to this:

NSString *asyncRequest = [vdb SelectAsyncRequest: i : _outgoingMsgId];

You instead are creating a new instance, then immediately either generating or referencing another instance.

Second:

Your code is very hard to read and doesn't follow the Objective-C smalltalk conventions.

Variable names should begin with a lowercase letter. Method names should also start with lowercase letters. Class names and functions should begin with capital letters. It makes it difficult to read because I and many others have been trained to see capital letters and think CLASS NAME instead of POSSIBLE VARIABLE NAME. Just FYI

Finally, some of your methods take multiple parameters, like the one above. You really should add a prefix to each parameter so that it's easy to understand what the parameter is for. This:

[vdb SelectAsyncRequest: PARAMETER : PARAMETER];

would look much better if it was :

[vdb selectAsyncRequestForParameter: PARAMETER withOtherParameter:OTHERPARAM];

EDIT: I also don't think you need so many autorelease pools. The entire thing is wrapped in a big autorelease pool already.

EDIT2: I also see a lot of release calls that aren't necessary. In your UploadChunkWise method you are calling release on *data and *returnedData which are both already implicitly autoreleased. Methods that return objects to you will already have ownership given up and "handed over" to you. Essentially, those methods will do this:

NSData *data = [[NSData alloc] init];
return [data autorelease];

When you get it, if you want to keep it you will have to retain it yourself, otherwise it will be destroyed at the return of your method.

However, it is correct for you to call release on the NSString *result instance you created with -init.

Upvotes: 1

Related Questions