[英]Memory leak warning in 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
方法中使用前一個函數內部的任務時,沒有任何問題,但這是到了這一步。 前面的警告顯示在控制台中。 它們用於NSData
, NSString
, NSMutableArray
,以及我在此函數中啟動的所有內容。
如果要在后台線程中分配自動釋放的對象,則需要創建一個自動釋放池:
pool = [[NSAutoreleasePool alloc] init];
// Your code with autoreleased objects
[pool release]
您也應該刪除警報。 除main之外的任何線程的所有UI更改都將導致內存泄漏
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.