1

再生ボタンをタップすると、Xcode で「Thread 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 に答える