我是 IOS 新手,但我仍然不确定我是否遇到了错误,或者我做错了什么。尽管考虑到我得到的奇怪行为,我倾向于认为这是 IOS 尝试重用连接的一个错误。我想知道是否有人知道这一点以及是否有解决方法。
发生在启用了 IOS 6.1 ARC 的模拟器上。更奇怪的是我不能在手机上让它崩溃,所以现在我想知道我是否应该害怕这个。
我正在同时运行多个 http 连接,一旦连接完成或失败,我将开始一个新连接。到目前为止,我有 3 种情况:
案例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