boost::weak_ptr<T>.lock() Crashes with a SIGSEGV Segmentation Fault

 ̄綄美尐妖づ 提交于 2019-11-29 12:48:43

Most likely, you are violating one of the rules for proper use of smart pointers. Here are the most common smart pointer rules violations:

  1. An object must only be referenced through a single chain of smart pointers. Ideally, create a smart pointer with the object using make_shared and never use a raw pointer. But otherwise, create a smart pointer from a regular pointer only once.

  2. Only create a weak pointer from the object's strong pointer. (Or you can use shared_from_this if the object supports it.)

  3. An object must not be destroyed by calling delete while a smart pointer refers to it. Ideally, you would never call delete on an object that ever had any kind of smart pointer refer to it.

There are two other typical causes of problems like this. One is that you have a bug causing memory corruption such as array bounds overwrite, double free, access after free, and so on. You can check for a memory corruption bug with a tool like valgrind.

Lastly, you may have compiled your code incorrectly or compiled Boost incorrectly. For example, your platform might have compiler options that need to be enabled to compile thread-safe code. (You didn't mention your platform, so I can't give you specifics.)

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