调用pthread_mutex_destroy之后是否需要free

2014-11-24 07:30:16 · 作者: · 浏览: 0
假设我有以下代码来初始化互斥变量:
[cpp]
pthread_mutex_t *m = new pthread_mutex_t;
pthread_mutex_init(m, NULL);
使用结束后调用:
[cpp]
pthread_mutex_destroy(m);
那么我是否需要调用 free(m)
答:
你需要释放内存,pthread_mutex_destroy并不能为你释放。
为什么呢?
因为下面的调用方法是被允许的:
[cpp]
pthread_mutex_t m;
pthread_mutex_init(&m, NULL);
pthread_mutex_destroy(&m); /* Can't free &m. 这里是引用*/