我有一台用C++编写的服务器,它在osx上运行时会泄露Mach端口。具体来说,在运行top
时,我注意到它有大约50000(在#PORTS
下)。奇怪的是,我让它在一夜之间运行,第二天机器基本上已经死机(花了15分钟响应ctrl-c,没有接受新的ssh连接),所以IT不得不重新启动它。这样的泄漏是否会导致系统崩溃?它没有以root身份运行。我应该如何去调试Mach端口泄漏?
无论如何,有什么好的策略来寻找这种泄漏的原因?有没有好的工具?
我发现一个测试,当运行可靠泄漏5端口,但就是这样。
编辑:我发现我们的线程类创建了一个mach端口泄漏,但我不明白为什么。在构造函数中,我们有下面的代码:
// Initialize the default attributes.
if (0 != pthread_attr_init(&m_threadAttributes))
{
throw "blah";
}
// Set the thread to be joinable.
if (0 != pthread_attr_setdetachstate(&m_threadAttributes, PTHREAD_CREATE_JOINABLE))
{
pthread_attr_destroy(&m_threadAttributes);
throw "blah";
}
if (0 != pthread_create(
&m_thread,
&m_threadAttributes,
&StartThreadFunction,
reinterpret_cast<void*>(this)))
{
throw "blah";
}
而且我注意到端口数为进程调用pthread_create
,预计后上升的一个。
然后,后来我加入用下面的代码线程:
if (0 != pthread_join(m_thread, NULL))
{
throw "blah";
}
而且不会抛出异常,所以我只能假设pthread_join
返回0,因此成功,但在顶部没有按端口#不要下去。还有什么我需要做清理线程?
是的,我早些时候试过dtrace,它可能是一个痛苦,获得对本机的root权限...... – Bwmat