原来代码是这样的:
while(1){
temp_p = (char *)&Data_Pack;
read(ShuChuanFd1, temp_p, 1);
if((char)*temp_p == 0x23)
{
temp_p++;
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p++, 1);
count = Data_Pack.Package_L - 10;
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p++, 1);
buff = (char *)malloc(count);
Data_Pack.Package_p =(unsigned char *) buff;
for(i=0;i<count;i++)
{
read(ShuChuanFd1, buff, 1);
buff++;
}
temp_p=temp_p+4;
read(ShuChuanFd1, temp_p++, 1);
read(ShuChuanFd1, temp_p, 1);
checksum = Calculate_CheckSum(Data_Pack);
if((checksum == Data_Pack.Package_CheckSum) && (Data_Pack.Package_E == 0x55))
Recv_Process(Data_Pack);
free(buff-count); //循环几次这里就出错
}
}
后来我把malloc语句放到循环外边,把free语句改为buff=buff-count就可以了。可能就如seasoblue说的重复释放同一地址的原因。