idealvin / coost

A tiny boost library in C++11.
Other
3.97k stars 561 forks source link

线程频繁创建销毁出现内存泄露 #338

Closed 1046099253 closed 1 year ago

1046099253 commented 1 year ago

现象:在线程生命周期内使用LOG打印日志,线程释放后出现内存泄露

定位:问题可能是TLS使用指针造成的,线程释放后并不会调用析构函数

inline fastream& log_stream() {
    static __thread fastream* s = 0;
    return s ? *s : *(s = co::_make_static<fastream>(256));
}

inline ThreadAlloc* talloc() {
    static __thread ThreadAlloc* ta = 0;
    return ta ? ta : (ta = make_thread_alloc());
}
idealvin commented 1 year ago

频繁创建、销毁线程是设计上的错误。使用了 TLS 的内存分配器,一般不能在线程退出时释放资源,一个线程分配的内存,可能被其他线程使用。

1046099253 commented 1 year ago

感谢回复,频繁创建、销毁线程确实是设计上的错误。