我有一个用 c++ 编写的服务器,在 osx 上运行时会泄漏 Mach 端口。具体来说,在运行时top
我注意到它有大约 50000 (under #PORTS
) 。奇怪的是,我让它在一夜之间运行,第二天机器基本上死了(花了 15 分钟来响应 ctrl-c,不接受新的 ssh 连接)所以 IT 不得不重新启动它。这样的泄漏会导致这样的系统崩溃吗?它没有以root身份运行。
无论如何,寻找这种泄漏原因的一些好的策略是什么?有什么好的工具吗?
我发现一项测试在运行时可靠地泄漏了 5 个端口,但仅此而已。
编辑:我发现我们的线程类创建了一个马赫端口泄漏,但我不明白为什么。在构造函数中,我们有以下代码:
// 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";
}
而且我注意到在调用 to 之后进程的端口数增加了 1 pthread_create
,这是预期的。
然后,稍后我使用以下代码加入线程:
if (0 != pthread_join(m_thread, NULL))
{
throw "blah";
}
并且没有抛出异常,所以我只能假设pthread_join
返回 0 并因此成功,但顶部的端口数不会下降。我还需要做些什么来清理线程吗?