Domanda

When tap the play button, it crashes in Xcode with message "Thread 6 GCDAsyncSocket 0 objc_msgSend". Some times it just works, but most of the time I got this error. Any clue?

Snapshot: http://i.imgur.com/G2Nv4.png

Below are the code lines:

- (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
    }
}

Thanks!

È stato utile?

Soluzione

Resolved by retain the data object in writeCommand method:

-(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];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top