线程创建多了,没有释放。导致内存泄露。。。
int main() { int len; int on=1; // pMachList = CreateEmptyLinklist(); DataBaseInit(); ReadAdjust(); int *temp; socketSrv=socket(AF_INET,SOCK_STREAM,0); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(8000); addrSrv.sin_addr.s_addr=htonl(INADDR_ANY); memset(addrSrv.sin_zero,0,8); if((setsockopt(socketSrv,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0) { perror("setsockopt failed"); exit(EXIT_FAILURE); } if(bind(socketSrv,(struct sockaddr*)&addrSrv,sizeof(addrSrv))<0) perror("bind"); listen(socketSrv,100); len=sizeof(addrClt); while(1) { pthread_t pid; temp=(int *)malloc(sizeof(int)); *temp= accept(socketSrv,(struct sockaddr*)&addrClt,(socklen_t*)(&len)); printf("accept socket= %d ",*temp); if(-1==*temp){ printf("accept error "); continue; }
int err; err=pthread_create(&pid,NULL,(void *)f1,temp);//一个线程处理一个客户端 if (err != 0) printf("can't create thread: %s ", strerror(err)); } close(socketSrv); return 0; }
在线程中加入: pthread_detach(pthread_self());
线程结束的时候自动销毁。。。
void f1(int *socketCtl2)//利用pthread_create把参数传进来 { unsigned char buf[1024]; int len,i; memset(buf,0,1024); struct FileRecord temFile; unsigned short CalCRC; pthread_detach(pthread_self()); while(1) { len=recv(*socketCtl2,buf,1024,0); // printf("recv len : %d ",len); if(len<=0) { printf("recv error len=%d ",len); deleteMachNode(*socketCtl2); close(*socketCtl2); free(socketCtl2); pthread_exit(NULL); } } }