Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CFStreamCreatePairWithSocketToHost与ios6模拟器一起损坏?_Ios - Fatal编程技术网

CFStreamCreatePairWithSocketToHost与ios6模拟器一起损坏?

CFStreamCreatePairWithSocketToHost与ios6模拟器一起损坏?,ios,Ios,我注意到,在我的程序中使用CFStreamCreatePairWithSocketToHost时,我遇到了随机的EXC_BADACCESS错误,但只有在使用ios6时,当使用ios5模拟器(甚至在带有xcode4.5的ios6sdk中)时,一切正常。最后,我将问题提取到一个小测试程序中,然后执行该测试程序-如果使用该程序启用guard malloc,它会立即崩溃(请参见下面的崩溃)。这也会发生在带弧和不带弧的情况下 @interface PHAppDelegate : UIResponder &

我注意到,在我的程序中使用CFStreamCreatePairWithSocketToHost时,我遇到了随机的EXC_BADACCESS错误,但只有在使用ios6时,当使用ios5模拟器(甚至在带有xcode4.5的ios6sdk中)时,一切正常。最后,我将问题提取到一个小测试程序中,然后执行该测试程序-如果使用该程序启用guard malloc,它会立即崩溃(请参见下面的崩溃)。这也会发生在带弧和不带弧的情况下

@interface PHAppDelegate : UIResponder <UIApplicationDelegate, NSStreamDelegate>
{
    NSOutputStream*     mOutputStream;
    NSInputStream*      mInputStream;
}

@implementation PHAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString* testAddress = @"192.168.1.0";
    [self openWithHost:testAddress port:444];
    return YES;
}

- (void)openWithHost:(NSString*)host port:(int)port
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       (CFStringRef)host, /*ip_addr*/
                                       port,
                                       &readStream,
                                       &writeStream);

    mInputStream = (NSInputStream *)readStream;
    mOutputStream = (NSOutputStream *)writeStream;

    if (mInputStream == nil)
    {
        NSLog(@"couldn't create the inputStream using CFStreamCreatePairWithSocketsToHost()");
        return;
    }

    if (mOutputStream == nil)
    {
        NSLog(@"couldn't create the outputstream using CFStreamCreatePairWithSocketsToHost()");
        return;
    }

    [mInputStream setDelegate:self];
    [mOutputStream setDelegate:self];

    [mInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [mOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [mInputStream open];
    [mOutputStream open];
}

#pragma mark NSStream delegate methods

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    NSLog(@"stream-handleEvent");
}

@end
@接口代理:UIResponder
{
NSOutputStream*mOutputStream;
NSInputStream*mInputStream;
}
@执行代理
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
NSString*testAddress=@“192.168.1.0”;
[使用主机自打开:测试地址端口:444];
返回YES;
}
-(void)openWithHost:(NSString*)主机端口:(int)端口
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
(CFStringRef)主机,/*ip地址*/
港口,
&读流,
&writeStream);
mInputStream=(NSInputStream*)readStream;
mOutputStream=(NSOutputStream*)writeStream;
if(mInputStream==nil)
{
NSLog(@“无法使用CFStreamCreatePairWithSocketsToHost()创建inputStream”;
返回;
}
if(mOutputStream==nil)
{
NSLog(@“无法使用CFStreamCreatePairWithSocketsToHost()创建输出流”;
返回;
}
[mInputStream setDelegate:self];
[mOutputStream setDelegate:self];
[mInputStream ScheduleRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[mOutputStream ScheduleUnloop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[mInputStream open];
[mOutputStream open];
}
#pragma标记NSStream委托方法
-(void)流:(NSStream*)aStream handleEvent:(NSStreamEvent)事件代码
{
NSLog(@“stream handleEvent”);
}
@结束
com.apple.networking.connection线程崩溃:

#0  0x04b35140 in tcp_connection_destination_prepare_complete ()
#1  0x04b34fee in tcp_connection_destination_start ()
#2  0x04b34c2b in tcp_connection_start_next_destination ()
#3  0x04b33c70 in tcp_connection_handle_reachability_changed ()
#4  0x04b30a95 in __tcp_connection_start_block_invoke_0 ()
#5  0x049fa53f in _dispatch_call_block_and_release ()
#6  0x04a0c014 in _dispatch_client_callout ()
#7  0x049fc418 in _dispatch_queue_drain ()
#8  0x049fc2a6 in _dispatch_queue_invoke ()
#9  0x049fd280 in _dispatch_root_queue_drain ()
#10 0x049fd450 in _dispatch_worker_thread2 ()
#11 0x94e7de12 in _pthread_wqthread ()
#12 0x94e65cca in start_wqthread ()

EXC_BADACCESS @ address 0x04b35140
0x04b3513b  <+0072>  call   0x4b332de <tcp_connection_destination_list_remove>
0x04b35140  <+0077>  mov    0x28(%esi),%eax
0x04b35143  <+0080>  test   %eax,%eax
#0 0x04b35140在tcp_连接_目标_准备_完成()
#1 0x04B34 tcp\连接\目的地\启动中的费用()
#tcp连接中的2 0x04b34c2b开始下一个目标()
#3 0x04b33c70在tcp中\u连接\u句柄\u可达性\u已更改()
#4 0x04b30a95在tcp连接启动块调用0()中
#5 0x049fa53f处于调度呼叫阻塞和释放状态()
#6 0x04a0c014在调度客户调用中()
#7 0x049fc418位于调度队列排液口()
#8 0x049fc2a6在调度队列调用()中
#9 0x049fd280位于\u调度\u根\u队列\u排水管()
#10 0x049fd450英寸调度工人螺纹2()
#11 0x94e7de12 in_pthread_wqthread()
#启动线程()中的12 0x94e65cca
EXC_BADACCESS@address 0x04b35140
0x04b3513b调用0x4b332de
0x04b35140 mov 0x28(%esi),%eax
0x04b35143测试%eax,%eax

我在iOS 5.x和iOS 6.x中有非常相似的代码。唯一的区别是,在调用
CFStreamCreatePairWithSocketToHost
之前,我将
CFReadStreamRef
CFWriteStreamRef
初始化为
NULL
,并为分配器传递
NULL
。为了方便起见,我还通常将代码作为一个类别添加到NSStream中。代码如下所示:

+ (void)createStreamsToHostNamed:(NSString*)hostName port:(NSInteger)port inputStream:(NSInputStream* __autoreleasing *)inputStream outputStream:(NSOutputStream* __autoreleasing *)outputStream {
    CFReadStreamRef readStream = NULL;
    CFWriteStreamRef writeStream = NULL;

    // Create a pair of of streams for a socket to the host specified
    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)hostName, port, &readStream, &writeStream);

    // Assign the output parameters
    *inputStream  = (__bridge_transfer NSInputStream*)readStream;
    *outputStream = (__bridge_transfer NSOutputStream*)writeStream;
}
@interface SomeClass : NSObject <NSStreamDelegate>
@end

@implementation SomeClass {
    NSInputStream* _inputStream;
    NSOutputStream* _outputStream;
}

- (void)_setupMethod {
    __autoreleasing NSInputStream* autoreleasingInputStream = nil;
    __autoreleasing NSOutputStream* autoreleasingOutputStream = nil;
    [NSStream createStreamsToHostNamed:kHostConstant port:kPortConstant inputStream:&autoreleasingInputStream outputStream:&autoreleasingOutputStream];

    if(autoreleasingInputStream != nil && autoreleasingOutputStream != nil) {
        _inputStream = autoreleasingInputStream;
        [_inputStream setDelegate:self];
        [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        _outputStream = autoreleasingOutputStream;
        [_outputStream setDelegate:self];
        [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        [_inputStream open];
        [_outputStream open];
    }
}

// NSStreamDelegate methods ...

@end
您可以使用以下代码调用该方法:

+ (void)createStreamsToHostNamed:(NSString*)hostName port:(NSInteger)port inputStream:(NSInputStream* __autoreleasing *)inputStream outputStream:(NSOutputStream* __autoreleasing *)outputStream {
    CFReadStreamRef readStream = NULL;
    CFWriteStreamRef writeStream = NULL;

    // Create a pair of of streams for a socket to the host specified
    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)hostName, port, &readStream, &writeStream);

    // Assign the output parameters
    *inputStream  = (__bridge_transfer NSInputStream*)readStream;
    *outputStream = (__bridge_transfer NSOutputStream*)writeStream;
}
@interface SomeClass : NSObject <NSStreamDelegate>
@end

@implementation SomeClass {
    NSInputStream* _inputStream;
    NSOutputStream* _outputStream;
}

- (void)_setupMethod {
    __autoreleasing NSInputStream* autoreleasingInputStream = nil;
    __autoreleasing NSOutputStream* autoreleasingOutputStream = nil;
    [NSStream createStreamsToHostNamed:kHostConstant port:kPortConstant inputStream:&autoreleasingInputStream outputStream:&autoreleasingOutputStream];

    if(autoreleasingInputStream != nil && autoreleasingOutputStream != nil) {
        _inputStream = autoreleasingInputStream;
        [_inputStream setDelegate:self];
        [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        _outputStream = autoreleasingOutputStream;
        [_outputStream setDelegate:self];
        [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        [_inputStream open];
        [_outputStream open];
    }
}

// NSStreamDelegate methods ...

@end
@接口SomeClass:NSObject
@结束
@类的实现{
NSInputStream*_inputStream;
NSOutputStream*_outputStream;
}
-(无效){
__自动释放NSInputStream*自动释放InputStream=nil;
__自动删除NSOutputStream*自动删除NSOutputStream=nil;
[NSStream CreateStreamStoHostName:kHostConstant端口:kPortConstant inputStream:&autoreleasingInputStream输出流:&autoreleasingOutputStream];
if(autoreleasingoutstream!=nil&&autoreleasingOutputStream!=nil){
_inputStream=自动释放inputStream;
[\u inputStream setDelegate:self];
[_InputStreamScheduleUnloop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
_outputStream=autoreleasingOutputStream;
[\u outputStream setDelegate:self];
[\u outputStream ScheduleUnloop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[_inputstreamopen];
[_outputstreamopen];
}
}
//NSStreamDelegate方法。。。
@结束

我发现同样的问题发生在iOS 6上,但在iOS 5上没有,您有任何更新吗?