当前位置: 技术问答>linux和unix
调用ACE_SSL_SOCK_Acceptor::accept函数的时候,进程死掉(在一个死循环中不断的调用accept,程序运行一段时间后,就会死掉)
来源: 互联网 发布时间:2016-04-21
本文导语: ACE_SSL_SOCK_Acceptor twoway_acceptor; ACE_INET_Addr twoway_server_addr (port); if (twoway_acceptor.open (twoway_server_addr, 100) == -1) ACE_ERROR_RETURN ((LM_ERROR,"%pn","open"),1); else if (twoway_acceptor.get_local_addr(twoway_server_addr) == -1) ACE_ERROR_RETURN...
ACE_SSL_SOCK_Acceptor twoway_acceptor;
ACE_INET_Addr twoway_server_addr (port);
if (twoway_acceptor.open (twoway_server_addr, 100) == -1)
ACE_ERROR_RETURN ((LM_ERROR,"%pn","open"),1);
else if (twoway_acceptor.get_local_addr(twoway_server_addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,"%pn","get_local_addr"),1);
ACE_Handle_Set handle_set;
handle_set.set_bit (twoway_acceptor.get_handle ());
for (;;)
{
ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
ACE_Time_Value time_com(20,0);
ACE_Handle_Set temp = handle_set;
int result = ACE_OS::select (int (twoway_acceptor.get_handle ()) + 1,(fd_set *) temp,0,0,&timeout);
if (result == -1) continue;
else if (result == 0 && verbose) continue;
else
{
if (temp.is_set (twoway_acceptor.get_handle ()))
{
ACE_SSL_SOCK_Stream* new_stream = new ACE_SSL_SOCK_Stream();
int r = twoway_acceptor.accept (*new_stream,0,&time_com,0); //程序到此处有时会死掉
if (r == -1)
{
new_stream->close();
delete new_stream;
continue;
}
ACE_INET_Addr remote_addr;
if (new_stream->get_remote_addr(remote_addr) == -1)
{
new_stream->close();
delete new_stream;
continue;
}
if (strcmp(remote_addr.get_host_addr(),m_ip))
{
new_stream->close();
delete new_stream;
//s_net.on_exp(EXP_WARNING,(string("manager ip address incorrect,but it has the manager certs, it ip is ")+remote_addr.get_host_addr()).c_str());
s_net.on_exp(EXP_WARNING,(string("管理IP不正确,但是对方有管理证书,对方IP是")+remote_addr.get_host_addr()).c_str());
_LOG("manager ip address incorrect,ignore");
continue;
}
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if (pthread_create(&tid,&attr,on_command,(void*)new_stream) != 0)
{
new_stream->close();
delete new_stream;
//s_net.on_exp(EXP_ERROR,"create task error on new manager coming,can't process manager command now");
s_net.on_exp(EXP_ERROR,"管理任务创建失败,现在不能处理管理命令。");
_LOG("creat new task command thread faild!");
continue;
}
}
}
以上是代码,大体上是在一个死循环中连续不断的去accept连接请求,但是程序运行一段时间后就自动死掉,不知道什么原因。请大侠们指点啊。
ACE_INET_Addr twoway_server_addr (port);
if (twoway_acceptor.open (twoway_server_addr, 100) == -1)
ACE_ERROR_RETURN ((LM_ERROR,"%pn","open"),1);
else if (twoway_acceptor.get_local_addr(twoway_server_addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,"%pn","get_local_addr"),1);
ACE_Handle_Set handle_set;
handle_set.set_bit (twoway_acceptor.get_handle ());
for (;;)
{
ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
ACE_Time_Value time_com(20,0);
ACE_Handle_Set temp = handle_set;
int result = ACE_OS::select (int (twoway_acceptor.get_handle ()) + 1,(fd_set *) temp,0,0,&timeout);
if (result == -1) continue;
else if (result == 0 && verbose) continue;
else
{
if (temp.is_set (twoway_acceptor.get_handle ()))
{
ACE_SSL_SOCK_Stream* new_stream = new ACE_SSL_SOCK_Stream();
int r = twoway_acceptor.accept (*new_stream,0,&time_com,0); //程序到此处有时会死掉
if (r == -1)
{
new_stream->close();
delete new_stream;
continue;
}
ACE_INET_Addr remote_addr;
if (new_stream->get_remote_addr(remote_addr) == -1)
{
new_stream->close();
delete new_stream;
continue;
}
if (strcmp(remote_addr.get_host_addr(),m_ip))
{
new_stream->close();
delete new_stream;
//s_net.on_exp(EXP_WARNING,(string("manager ip address incorrect,but it has the manager certs, it ip is ")+remote_addr.get_host_addr()).c_str());
s_net.on_exp(EXP_WARNING,(string("管理IP不正确,但是对方有管理证书,对方IP是")+remote_addr.get_host_addr()).c_str());
_LOG("manager ip address incorrect,ignore");
continue;
}
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if (pthread_create(&tid,&attr,on_command,(void*)new_stream) != 0)
{
new_stream->close();
delete new_stream;
//s_net.on_exp(EXP_ERROR,"create task error on new manager coming,can't process manager command now");
s_net.on_exp(EXP_ERROR,"管理任务创建失败,现在不能处理管理命令。");
_LOG("creat new task command thread faild!");
continue;
}
}
}
以上是代码,大体上是在一个死循环中连续不断的去accept连接请求,但是程序运行一段时间后就自动死掉,不知道什么原因。请大侠们指点啊。
|
代码好乱哦,
看起来是只要select成功以就new了一个ACE_SSL_SOCK_Stream;
但是你的程序中并没有保证所有new出来的ACE_SSL_SOCK_Stream都delete了
看起来是只要select成功以就new了一个ACE_SSL_SOCK_Stream;
但是你的程序中并没有保证所有new出来的ACE_SSL_SOCK_Stream都delete了
|
那你有没有做同步呢?
因为你子线程里要的是new_stream指向的地址,而你主线程可能很快的new了一个新的空间这样new_stream指向了一个新的地址。
因为你子线程里要的是new_stream指向的地址,而你主线程可能很快的new了一个新的空间这样new_stream指向了一个新的地址。
|
你的on_command里面有没有把new_stream给释放掉的动作?