- 论坛徽章:
- 0
|
以下代码频繁执行,内存一直在增长,自己找不到问题,请高手指教以下,感谢!
static void *local_notify_thread(void *arg)
{
pthread_detach(pthread_self());
unsigned int cnt_disp_id = 0;
//get param
char *id_oper = (char *)arg;
char notify_ms_id[22];
bzero(notify_ms_id,21);
unsigned int notify_oper = 0;
bcopy(id_oper,notify_ms_id,21);
if (strncmp(id_oper+21,"3",1) == 0){
notify_oper = 3;//online
}else{
notify_oper = 4;//offline
}
free(arg);
if(id_oper !=NULL){
free(id_oper);
id_oper = NULL;
}
//get counts of need to send notify to disp
int i;
Pthread_mutex_lock(&ssc_disp_entries.disp_entries_mutex);
//get need to send notify disp's count&socket&disp_id
dispinfo *tmp_ci_disps = NULL;
for (i = 0;i < 500;i++){
if(strncmp(ssc_disp_entries.disp_n[i].disp_id,"860020",6) == 0 )
{
if ((ssc_disp_entries.disp_n[i].disp_sock > 0)
&&(strncmp(ssc_disp_entries.disp_n[i].disp_id+12,notify_ms_id+12,3)== 0)
&&(strncmp(ssc_disp_entries.disp_n[i].disp_id,notify_ms_id,21) != 0))
{
cnt_disp_id++;
if(tmp_ci_disps == NULL){
tmp_ci_disps = (dispinfo *)malloc(sizeof(dispinfo)*cnt_disp_id);
}else{
tmp_ci_disps = (dispinfo *)realloc(tmp_ci_disps,sizeof(dispinfo)*cnt_disp_id);
}
bcopy(ssc_disp_entries.disp_n[i].disp_id, tmp_ci_disps[cnt_disp_id-1].disp_id, 21);
tmp_ci_disps[cnt_disp_id-1].disp_sock = ssc_disp_entries.disp_n[i].disp_sock;
}
}else{
break;
}
}
//get need to send notify disp's info
dispinfo *ci_disps = NULL;
ci_disps = (dispinfo *)calloc(cnt_disp_id,sizeof(dispinfo));
int s;
for (s =0; s <cnt_disp_id;s++){
bcopy(tmp_ci_disps[s].disp_id,ci_disps[s].disp_id,21);
ci_disps[s].disp_sock =tmp_ci_disps[s].disp_sock;
}
if(tmp_ci_disps !=NULL){
free(tmp_ci_disps);
tmp_ci_disps = NULL;
}
Pthread_mutex_unlock(&ssc_disp_entries.disp_entries_mutex);
int ret_select=0;
int reachable_count=0;
struct pollfd *ppfds;
ppfds = (struct pollfd *)malloc(sizeof(struct pollfd)* cnt_disp_id);
if(ppfds == NULL) {
if(ci_disps != NULL) {
free(ci_disps);
ci_disps = NULL;
}
return (NULL);
}
int j;
for(j=0;j<cnt_disp_id;j++){
ppfds[reachable_count].fd = ci_disps[j].disp_sock;
ppfds[reachable_count].events = POLLOUT;
reachable_count++;
}
while(reachable_count>0){
ret_select=poll(ppfds, reachable_count, INFTIM);
if(ret_select<=0)
{
#ifdef _OUTPUT
printf(" poll error:%d\n",ret_select);
#endif
break;
} else{
int k=0;
for(k=0; k<reachable_count; ++k) {
if(ppfds[k].revents) {
int kk;
for(kk=0; kk < cnt_disp_id; kk++) {
if(ppfds[k].fd == ci_disps[kk].disp_sock) {
int n;
char write_buf[MAXLINE];
bzero(write_buf, MAXLINE);
int ret_sspp = 0;
ret_sspp = mk_notify(write_buf, 0, 0, notify_ms_id,notify_oper, notify_ms_id,
NULL, NULL, 0, 0, 0, NULL);
if(ret_sspp != 0){
printf("mk_notify error!ret_sspp:%d\n",ret_sspp);
}else{
n = TOWrite(ci_disps[kk].disp_sock, write_buf, LEN_NOTIFY_MS,g_cp_serv_write_timeout);
#ifdef _OUTPUT
printf("poll notify send ms_id:%s socket:%d TOWrite:%d\n", ci_disps[kk].disp_id,ci_disps[kk].disp_sock,n);
#endif
}
//Clear the fdset
ppfds[k].events = 0;
}
}
}
}
reachable_count=reachable_count-ret_select;
}
}//end while
if(ci_disps != NULL) {
free(ci_disps);
ci_disps = NULL;
}
free(ppfds);
ppfds = NULL;
return (NULL);
} |
|