Cleaning up in Shutdown() method instead of destructor

£可爱£侵袭症+ 提交于 2019-12-04 10:41:01
BЈовић

Generally, it is a bad advice not to do a cleanup in the destructor.

But you can do it if the clean operation can fail, and you want to throw an exception. Then you have to be careful, as another exception would call abort(). For that particular case, doing cleanup in a separate function makes sense.

By the way, the code example indeed looks like from someone coming from the c world.

The reason being is that I don't trust it to be called. Certain windows functions like ExitThread() are known for not calling your class destructors resulting in memory leaks.

That is correct, but try to avoid such functions. Also from here :

There is no portable way in C++11 (that I'm aware of) to non-cooperatively kill a single thread in a multi-thread program (i.e. without killing all threads).

therefore just let the thread nicely finish, and destructors will be called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!