开发者

Memory leak warning in iPhone

Object 0x3d58870 of class NSCFString autoreleased with no pool in place - just leaking

I'm receiving this memory warning for almost all variables used in the following function:

- (void) backgroundTask {

c= [array1 count];
if (c == 0){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ERROR" message:@"Chose an image to upload!!!" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
else {  
    for(k==0;k<c;k++){
开发者_如何学JAVA        [uploading startAnimating];

        upload = [array1 objectAtIndex:0];
        NSData *imageData = UIImageJPEGRepresentation(upload, .9);      
        NSString *urlString = @"http://fileserver.colormailer.com/fileserver/photoService?method=addPhoto&description=testingupload&albumId=nithinalbum&userId=123nithin&sid=12345678&title=image125";

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        msg = [[NSString alloc]initWithFormat:@"Upload number %d",a];
        NSLog(msg);
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(returnString);
        a++;
        [returnString release];
        [array1 removeObjectAtIndex:0];

    }
    [uploading stopAnimating];
    if(c == 1){
        msg = [[NSString alloc]initWithFormat: @"1 image uploaded"];
    }
    else{
        msg = [[NSString alloc]initWithFormat: @"%d images uploaded",c];
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"upload success" message:msg delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil];
    [alert show];
    [msg release];
    [alert release];
}
}

I'm calling this function as:

- (IBAction)pushUpload {
    [self performSelectorInBackground:@selector(backgroundTask) withObject:nil];
}

When I used the tasks inside the former function directly in the Action method, there were no problems, but it came for this step. The aforementioned warnings are shown in the console. They come for NSData, NSString, NSMutableArray, and everything I've initiated in this function...


If you're allocating autoreleased objects in a background thread you'll need to create an autorelease pool:

pool = [[NSAutoreleasePool alloc] init];

// Your code with autoreleased objects

[pool release]


also you should remove your alerts. all UI changes from any thread except main, will cause memory leaks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜