NSURLConnection崩溃同时失败的连接IOS6.1 ARC模拟器

时间:2013-08-22 18:51:47

标签: ios objective-c

我是IOS的新手,我仍然不确定我是否遇到了错误,或者我做错了什么。虽然我得到了奇怪的行为,但我倾向于认为这是IOS如何尝试重用连接的一个错误。我想知道是否有人知道这个以及是否有解决方法。

发生在IOS 6.1 ARC启用的模拟器上。更奇怪的是我无法通过电话将其崩溃,所以现在我想知道我是否应该害怕这一点。

我正在运行多个同时的http连接,一旦连接完成或失败,我就开始一个新的连接。到目前为止,我有三种情况:

案例1:服务器存在且有效,每个请求成功,有效。

案例2:服务器存在但不会侦听给定端口,无效。

案例3:服务器处于脱机状态(例如,机器已关闭),在100-2000个请求后将崩溃。

编辑:根据NicholasHart建议修改代码,问题仍然存在

#import "ViewController.h"

@interface ViewController ()
{
    NSURLRequest *request;
}
@end

@implementation ViewController

- (IBAction)test:(id)sender {
    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.178:8888/r.php"]
                                                    cachePolicy:NSURLCacheStorageNotAllowed
                                                timeoutInterval:10.0];
    NSLog(@"starting %@", [NSThread currentThread]);
    NSURLConnection *conn;

    for(int i=0;i<6;i++) conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse*)cachedResponse {return nil;}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //[connection cancel]; //not needed, suggested by NicholasHart
    NSLog(@"ok %@", [NSThread currentThread]);
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    //[connection cancel]; //not needed, suggested by NicholasHart
    NSLog(@"error %@", [NSThread currentThread]);
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
@end

对于案例3,我有不同的崩溃类似于:


libdispatch.dylib`_dispatch_retain:
0x49f98f0:  movl   4(%esp), %eax
0x49f98f4:  cmpl   $2147483647, 4(%eax)
0x49f98fb:  je     0x49f9910                 ; _dispatch_retain + 32
0x49f98fd:  addl   $4, %eax
0x49f9900:  movl   $1, %ecx
0x49f9905:  lock
0x49f9906:  xaddl  %ecx, (%eax)
0x49f9909:  incl   %ecx
0x49f990a:  testl  %ecx, %ecx
0x49f990c:  jg     0x49f9910                 ; _dispatch_retain + 32
0x49f990e:  ud2
0x49f9910:  ret

0 个答案:

没有答案