iPhone中的内存泄漏警告

时间:2009-12-11 10:30:29

标签: iphone

  

类NSCFString的对象0x3d58870自动释放,没有池到位 - 只是泄漏

我收到以下函数中使用的几乎所有变量的内存警告:

- (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++){
        [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];
}
}

我将此功能称为:

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

当我在Action方法中直接使用前一个函数内的任务时,没有任何问题,但是这个步骤就出现了。 上述警告显示在控制台中。 它们来自NSDataNSStringNSMutableArray以及我在此功能中发起的所有内容......

2 个答案:

答案 0 :(得分:5)

如果您在后台线程中分配自动释放的对象,则需要创建自动释放池:

pool = [[NSAutoreleasePool alloc] init];

// Your code with autoreleased objects

[pool release]

答案 1 :(得分:2)

您也应该删除提醒。除main之外的任何线程的所有UI更改都会导致内存泄漏