1

当点击播放按钮时,它在 Xcode 中崩溃并显示消息“线程 6 GCDAsyncSocket 0 objc_msgSend”。有时它只是工作,但大多数时候我得到这个错误。有什么线索吗?

快照:http: //i.imgur.com/G2Nv4.png

以下是代码行:

- (void)viewDidLoad {
    [self initSocket];
}

-(void) initSocket
{
    dispatch_queue_t mainQueue =  dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
    [self connectToVLC];
}

-(void) connectToVLC
{
    NSString *host = @"192.168.0.8";
    uint16_t port = 8080;
    NSError *error = nil;

    if (![asyncSocket connectToHost:host onPort:port error:&error]){
        NSLog(@"unable connect to VLC server");
    }else{
        NSLog(@"connected to VLC server");
    }
}

-(IBAction) btnPlayTapped
{
    [self writeCommand:@"stop" tag:TAG_NONE];
    [self writeCommand:@"repeat off" tag:TAG_NONE];
    [self writeCommand:@"loop off" tag:TAG_NONE];
    [self writeCommand:@"add /abc.wav" tag:TAG_NONE];
    [self writeCommand:@"play" tag:TAG_PLAY];
}

-(void) writeCommand:(NSString *)cmd tag:(long)theTag
{
    NSMutableData *data = [NSMutableData dataWithData:[cmd dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[GCDAsyncSocket CRLFData]];
    [asyncSocket writeData:data  withTimeout:-1 tag:theTag];
}


- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    if(tag==TAG_PLAY){
        //handle rest operation here
    }
}

谢谢!

4

1 回答 1

1

通过在 writeCommand 方法中保留数据对象解决:

-(void) writeCommand:(NSString *)cmd tag:(long)theTag
{
    NSMutableData *data = [NSMutableData dataWithData:[cmd dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[GCDAsyncSocket CRLFData]];
    [data retain];//release it in dealloc
    [asyncSocket writeData:data  withTimeout:-1 tag:theTag];
}
于 2012-11-08T00:29:21.527 回答