开发者

My Thread Programs Crash

开发者 https://www.devze.com 2022-12-26 11:59 出处:网络
I have a problem with threads objectiveC. The line of code below contains the recv bloc开发者_如何学Gok the program waiting for a datum.

I have a problem with threads objectiveC. The line of code below contains the recv bloc开发者_如何学Gok the program waiting for a datum. My intention is to launch a thread parallel to the program so that this statement does not block any application. I put this code in my program but when active switch the program crashes. Enter the code.

-(IBAction)Chat{

    if(switchChat.on){

        buttonInvio.enabled = TRUE;
        fieldInvio.enabled = TRUE;

        [NSThread detachNewThreadSelector:@selector(riceviDatiServer) toTarget:self withObject:nil];  

    }
    else {
        buttonInvio.enabled = FALSE;
        fieldInvio.enabled = FALSE;
    }

-(void)riceviDatiServer{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    int ricevuti;
    NSString *datiRicevuti;
    ricevuti = recv(temp, &datiRicevuti, datiRicevuti.length, 0);
    labelRicezione.text = [[NSString alloc] initWithFormat:@"%s.... %d", datiRicevuti, ricevuti];

    [pool   release];

}


This part

 NSString *datiRicevuti;
 ricevuti = recv(temp, &datiRicevuti, datiRicevuti.length, 0);

is clearly bad. NSString* is not a C buffer. So you shouldn't pass that to recv. What you should is to recv the data just as in C (see the documentation for recv). Say it's now in void*receivedData and its length is dataLength. Then, convert it to NSString by something like

NSString*dataAsNSString=[[NSString alloc] initWithBytes:receivedData 
                                          length:dataLength encoding:NSISOLatin1StringEncoding];
0

精彩评论

暂无评论...
验证码 换一张
取 消