繁体   English   中英

线程池中的分段错误

[英]Segmentation fault in Thread pool

我正在Bluez中创建一个基于蓝牙的服务器程序,该程序基本上可以处理来自多个设备的连接。 我构造了两个线程; 一个用于扫描远程设备,另一个用于与正在广播服务的设备连接。 同样,从线程池中为每个新连接的设备提取一个单独的线程,这些设备随后将通过RFCOMM通道与服务器进行通信。

与远程设备建立连接后,服务器将向远程蓝牙设备发送命令。 远程设备回复后,服务器将读取该回复并将其存储。

现在的问题是,每当我从设备收到回复时,程序就会崩溃,并指出“分段错误”。 谁能告诉我一个可能的原因。 此处提供了执行此操作的代码部分。

void startCommunication( int newSocket )
{


    char buf[MODZ_MAX_DATA_SIZE] = "\0";
        char previousData[ MODZ_MAX_DATA_SIZE ] = "\0"; 
        time_t recvTime, prevRecvTime;
        char dataToBeSent[ 4*MODZ_MAX_DATA_SIZE ] = "\0";
        char *result;

if( sendDataToClient( newSocket, CMD_SEND) == EXT_ERROR )   //send acknowledgement first
    printf("Couldn;t send ack\n");
else { printf("Date send woot! woot! %s\n", CMD_SEND); } 

memset( buf, '0', sizeof(buf) );

while( 1 ){
recvTime = time( ( time_t * )0 );

    if( readDataFromClient( newSocket, buf ) == EXT_ERROR ){
        printf( "Read Error\n" );
        break;
    }
    printf( "Data received = %s\n", buf );

    strcpy( previousData, buf );

    // store the data in a file and send to web

    // check if the web has any data to send and if there is then send
    result = "here we update the challenge";
    strcpy( dataToBeSent, result );
    free( result );
    result = NULL;

    //strcpy( buf, "We will soon update the database" );
    if( sendDataToClient( newSocket, dataToBeSent ) == EXT_ERROR ){
        break;
    }

    }           
close( newSocket );

if( result != NULL ){
    free( result );

}

printf( "\n****************Device disconnected***************\n" );

}

一个明显的问题:

result = "here we update the challenge";
strcpy( dataToBeSent, result );
free( result );

您正在释放未使用malloc分配的指针。 那很可能导致分段错误。

将来,尝试使用gdb找出程序崩溃的确切位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM