iPhone:自定义自动释放池中的崩溃
Posted
技术标签:
【中文标题】iPhone:自定义自动释放池中的崩溃【英文标题】:iPhone: Crash in Custom Autorelease Pool 【发布时间】:2010-06-14 14:58:46 【问题描述】:当我尝试在 HTTP 请求中发布图像时,我的应用程序崩溃了。我正在尝试将图像上传到服务器。崩溃似乎与我的自动释放池有关,因为崩溃被困在[pool release
] 消息中。
这是崩溃报告:
#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
崩溃报告说以下代码的最后一行是崩溃点。 (第 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 *urlString=@"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
我不明白导致崩溃的原因。
【问题讨论】:
您确实需要通过点击旁边的复选标记来接受一些答案。在一个月的时间里,你问了 8 个问题,但没有接受任何问题。这使人们不太可能花时间找出您的问题,并且会混淆 *** 系统。 请不要在我花了 15 分钟编辑后关闭它。 【参考方案1】:您在这里不需要自定义自动释放池。除非您一次保存数十张图像(不太可能),否则不需要本地池。
在循环之外创建自动释放池没有多大意义,因为无论如何循环都包含整个方法。如果您确实需要自定义池,则应将其放在循环中,以便每次通过循环时清理内存。
崩溃可能与池没有直接关系,但这只是调试器捕获它的地方。要开始调试,请注释掉自动释放池及其耗尽。这可能会发现崩溃的根源。
作为一个好的经验法则。在您测试代码段并确定它需要一个本地池之前,不要创建本地池。很少需要,只有当您在小范围内创建大量对象时才需要。
【讨论】:
【参考方案2】:根据您的崩溃报告,很难找出崩溃发生的原因。不确定您是否可以使用 NSLog 来获取一些调试信息(至少在您的示例代码中不能)? NSLog 是一个将消息输出到输出控制台的函数。这是找出崩溃位置的好工具。
【讨论】:
以上是关于iPhone:自定义自动释放池中的崩溃的主要内容,如果未能解决你的问题,请参考以下文章