0

當我嘗試在HTTP請求中發佈圖像時,我的應用程序崩潰。我正嘗試將圖像上傳到服務器。該崩潰似乎與我的autorelease池相關,因爲崩潰被困在[pool release]消息中。iPhone:在自定義Autorelease池中崩潰

這裏是崩潰報告:

#0 0x326712f8 in prepareForMethodLookup() 
#1 0x3266cf5c in lookUpMethod() 
#2 0x32668f28 in objc_msgSend_uncached() 
#3 0x33f70996 in NSPopAutoreleasePool() 
#4 0x33f82a6c in -[NSAutoreleasePool drain]() 
#5 0x00003d3e in -[CameraViewcontroller save:] (self=0x811400, _cmd=0x319c00d4, number=0x11e210) at /Users/hardikrathore/Desktop/LiveVideoRecording/Classes/CameraViewcontroller.m:266 
#6 0x33f36f8a in __NSFireDelayedPerform() 
#7 0x32da44c2 in CFRunLoopRunSpecific() 
#8 0x32da3c1e in CFRunLoopRunInMode() 
#9 0x31bb9374 in GSEventRunModal() 
#10 0x30bf3c30 in -[UIApplication _run]() 
#11 0x30bf2230 in UIApplicationMain() 
#12 0x00002650 in main (argc=1, argv=0x2ffff474) at /Users/hardikrathore/Desktop/LiveVideoRecording/main.m:14 

的崩潰報告說,下面的代碼的最後一行是崩潰的地步。 (Line No.266)

-(void)save:(id)number 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    j =[number intValue]; 
    while(screens[j] != NULL){ 
     NSLog(@" image made : %d",j); 
     UIImage * image = [UIImage imageWithCGImage:screens[j]]; 
     image=[self imageByCropping:image toRect:CGRectMake(0, 0, 320, 240)]; 
     NSData *imgdata = UIImageJPEGRepresentation(image,0.3); 
     [image release]; 

     CGImageRelease(screens[j]); 
     screens[j] = NULL; 

     UIImage * image1 = [UIImage imageWithCGImage:screens[j+1]]; 
     image1=[self imageByCropping:image1 toRect:CGRectMake(0, 0, 320, 240)]; 
     NSData *imgdata1 = UIImageJPEGRepresentation(image1,0.3); 
     [image1 release]; 

     CGImageRelease(screens[j+1]); 
     screens[j+1] = NULL; 
     NSString *[email protected]"http://www.test.itmate4.com/iPhoneToServerTwice.php"; 
     // setting up the request object now 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     NSString *fileName=[VideoID stringByAppendingString:@"_"]; 
     fileName=[fileName stringByAppendingString:[NSString stringWithFormat:@"%d",k]]; 
     NSString *fileName2=[VideoID stringByAppendingString:@"_"]; 

     fileName2=[fileName2 stringByAppendingString:[NSString stringWithFormat:@"%d",k+1]]; 
     /* 
     add some header info now 
     we always need a boundary when we post a file 
     also we need to set the content type 

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html post 
     */ 
     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

     /* 
     now lets create the body of the post 
     */ 
     //NSString *count=[NSString stringWithFormat:@"%d",front];; 
     NSMutableData *body = [NSMutableData data]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
     //[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; count=\"@\"";filename=\"%@.jpg\"\r\n",count,fileName] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:imgdata]]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     //second boundary 

     NSString *string1 = [[NSString alloc] initWithFormat:@"\r\n--%@\r\n",boundary]; 
     NSString *string2 =[[NSString alloc] initWithFormat:@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"%@.jpg\"\r\n",fileName2]; 
     NSString *string3 =[[NSString alloc] initWithFormat:@"\r\n--%@--\r\n",boundary]; 


     [body appendData:[string1 dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[string2 dataUsingEncoding:NSUTF8StringEncoding]]; 
     //experiment 
     //[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"%@.jpg\"\r\n",fileName2] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:imgdata1]]; 
     //[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[string3 dataUsingEncoding:NSUTF8StringEncoding]]; 
     // setting the body of the post to the reqeust 
     [request setHTTPBody:body]; 
     // now lets make the connection to the web 
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
     if([returnString isEqualToString:@"SUCCESS"]) 
     { 
      NSLog(returnString); 
      k=k+2; 
      j=j+2; 
      [self performSelectorInBackground:@selector(save:) withObject:(id)[NSNumber numberWithInt:j]]; 
     } 

     [imgdata release]; 
     [imgdata1 release]; 

    [NSThread sleepForTimeInterval:.01]; 

    } 
    [pool drain];   //<-------------Line 266 
} 

我不明白是什麼導致了崩潰。

+1

你確實需要接受一些答案,點擊下面的複選標記。你在一個月的時間裏提出了8個問題,而且一個也不接受。這使得人們不太可能花時間計算出你的問題,並且會使計算器系統混淆。 – TechZen 2010-06-14 16:20:03

+0

編輯完15分鐘後請不要關閉它。 – TechZen 2010-06-14 18:30:21

回答

1

這裏你不需要自定義的自動釋放池。除非您一次存儲數十張圖像(不太可能),否則不需要本地存儲池。

由於循環包含整個方法,所以在循環外創建一個自動釋放池沒有多大意義。如果你確實需要一個自定義池,你應該把它放在循環中,這樣它就可以清理循環中的每一個內存。

崩潰可能與池沒有直接關係,但這只是調試器捕獲它的地方。要開始調試,註釋掉autorelease池和它的漏洞。這可能會揭露事故的根源。

作爲一個很好的經驗法則。在測試代​​碼段並確定它需要之前,不要創建本地池。只有很少的人需要,然後才能在小範圍內創建大量對象。

0

根據你的崩潰報告,很難找出墜毀發生的原因。不確定您是否可以使用NSLog獲取一些調試信息(至少不在您的示例代碼中)? NSLog是輸出消息到輸出控制檯的函數。這是一個很好的工具,找出崩潰的位置。