提高:: weak

编程入门 行业动态 更新时间:2024-10-18 16:46:51
本文介绍了提高:: weak_ptr的< T> .lock()与SIGSEGV段错误崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

(编辑)环境:

plee @ SOS的构建:在/ usr /本地/包括/升压$ lsb_release -a没有LSB模块可供选择。经销商ID:Ubuntu的说明:Ubuntu的11.10稿:11.10codeNAME:precise中plee @ SOS的构建:在/ usr /本地/包括/升压$的uname -aLinux的SOS-建设3.0.0-12泛型#20 Ubuntu的SMP周五10月7日14时56分25秒UTC 2011 x86_64的x86_64的x86_64的GNU / Linux的plee @ SOS的构建:在/ usr /本地/包括/升压$plee @ SOS的构建:在/ usr /本地/包括/升压$猫version.hpp// BOOST_LIB_VERSION必须被定义为相同BOOST_VERSION#定义BOOST_LIB_VERSION1_47

我一直在服务器端项目。我使用Boost库,如的boost :: ASIO ,的boost :: shared_ptr的和的boost :: weak_ptr的。

升压文档( HTTP:// WWW。 boost/doc/libs/1_47_0/libs/smart_ptr/weak_ptr.htm#lock )说,的weak_ptr< T> .lock 不会抛出:

  

锁定

    

shared_ptr的锁()const的;返回:过期()? shared_ptr的():  shared_ptr的(*此)。

    

抛出:没什么

不过,在我的应用程序,它甚至崩溃:

计划接收信号SIGSEGV,分割过错。[切换主题0x7fffeffff700(LWP 5102)]0x000000000066fe08在boost ::详细:: atomic_conditional_increment(PW = 0x800000000007)    在/usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:9292);(GDB)(GDB)BT#0 0x000000000066fe08中的boost ::详细:: atomic_conditional_increment(PW = 0x800000000007)    在/usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:92#1 0x000000000066fe5c在boost ::详细:: sp_counted_base :: add_ref_lock(此= 0x7fffffffffff)    在/usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:138#2 0x000000000068009b在boost ::详细:: SHARED_COUNT :: SHARED_COUNT(此= 0x7fffefffe658,R = ...)    在/usr/local/include/boost/smart_ptr/detail/shared_count.hpp:518升压#3 0x0000000000691599 :: shared_ptr的< RtmpConnection> :: shared_ptr的< RtmpConnection> (    此= 0x7fffefffe650,R = ...)在/usr/local/include/boost/smart_ptr/shared_ptr.hpp:216#4 0x000000000068db48在boost :: weak_ptr的< RtmpConnection> ::锁(此= 0x7fffe0e87e68)    在/usr/local/include/boost/smart_ptr/weak_ptr.hpp:157

我查了线坠毁在 /usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp

69内嵌INT atomic_conditional_increment为(int * PW) 70 { 71 // INT RV = * PW; 72 //如果(RV!= 0)++ * PW; 73 //返回rv; 74 75 INT RV,TMP; 76 77 __asm​​__ 78( 79MOVL%0 %% EAX \\ n \\ t的 800:\\ n \\ t的 81测试%% EAX,EAX %% \\ n \\ t的 82JE 1F \\ n \\ t的 83MOVL EAX %%%2 \\ n \\ t的 84含2%\\ n \\ t的 85锁\\ n \\ t的 86cmpxchgl%2%0 \\ n \\ t的 87JNE 0B \\ n \\ t的 881: 89= M(* PW),=&放大器;一个(RV),=&安培; R(TMP)://输出(%0%1%2) 90M(* PW)://输入(%3) 91CC//则会覆盖 92); 93 94返回rv; 95}

该生产线92装配code。我真的不知道这意味着什么。

我总是做检查,如果返回的的boost :: weakptr< RtmpConnection> .lock()(类型为中的boost :: shared_ptr的< RtmpConnection&GT ; 是空的之前,我用它

所以我一派,我看到这个 wiki.inkscape/wiki /index.php/Boost_shared_pointers

  

弱指针不能被解除引用线程安全的原因。如果  其它线程销毁对象你检查后弱  指针到期,但你使用它之前,你会得到一个崩溃

  • 所以,我应该怎么做才能对付它,为什么它会崩溃(似乎的boost :: weakptr< RtmpConnection> .lock()应该永不死机)
  • 由于我的程序是多线程的。这可能是后我得到检查升压的返回值:: weakptr< RtmpConnection> .lock()的 RtmpConnection 可能会被其他线程被摧毁,确实Boost库保证它不会被破坏,因为返回类型为的boost :: shared_ptr的< RtmpConnection>
  • 解决方案

    最有可能的,你违反了正确使用智能指针的规则之一。以下是最常见的智能指针违反规则:

  • 这是对象必须只能通过智能指针的单链引用。理想情况下,使用 make_shared 创建该对象的智能指针,从不使用原始指针。但除此之外,创建从一个普通的指针只有一次智能指针。

  • 只有创建对象的指针强一个弱指针。 (或者你可以使用 shared_from_this 如果对象支持它。)

  • 这是对象不能调用被销毁删除而智能指针引用它。理想情况下,你永远不会叫删除即过任何形式的智能指针引用它的对象上。

  • 有像这样的问题,其他两个典型原因。一个是你有一个bug导致内存损坏,如数组越界覆盖,双重释放,获得自由后,依此类推。您可以检查一个内存破坏漏洞像的valgrind 的工具。

    最后,你可能已经编译了code不正确或不正确编译升压。例如,您的平台可能需要启用编译线程安全code编译器选项。 (你没有提到你的平台,所以我不能给你具体细节。)

    (EDIT) Environment:

    plee@sos-build:/usr/local/include/boost$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric plee@sos-build:/usr/local/include/boost$ uname -a Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux plee@sos-build:/usr/local/include/boost$ plee@sos-build:/usr/local/include/boost$ cat version.hpp // BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION #define BOOST_LIB_VERSION "1_47"

    I have been working on a server-side project. I use boost libraries, such as, boost::asio, boost::shared_ptr, and boost::weak_ptr.

    The Boost documentation (www.boost/doc/libs/1_47_0/libs/smart_ptr/weak_ptr.htm#lock) says that the weak_ptr<T>.lock never throws:

    lock

    shared_ptr lock() const; Returns: expired()? shared_ptr(): shared_ptr(*this).

    Throws: nothing.

    However, in my application, it's even crashed:

    Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7fffeffff700 (LWP 5102)] 0x000000000066fe08 in boost::detail::atomic_conditional_increment (pw=0x800000000007) at /usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:92 92 ); (gdb) (gdb) bt #0 0x000000000066fe08 in boost::detail::atomic_conditional_increment (pw=0x800000000007) at /usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:92 #1 0x000000000066fe5c in boost::detail::sp_counted_base::add_ref_lock (this=0x7fffffffffff) at /usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:138 #2 0x000000000068009b in boost::detail::shared_count::shared_count (this=0x7fffefffe658, r=...) at /usr/local/include/boost/smart_ptr/detail/shared_count.hpp:518 #3 0x0000000000691599 in boost::shared_ptr<RtmpConnection>::shared_ptr<RtmpConnection> ( this=0x7fffefffe650, r=...) at /usr/local/include/boost/smart_ptr/shared_ptr.hpp:216 #4 0x000000000068db48 in boost::weak_ptr<RtmpConnection>::lock (this=0x7fffe0e87e68) at /usr/local/include/boost/smart_ptr/weak_ptr.hpp:157

    I checked the line crashed in /usr/local/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp

    69 inline int atomic_conditional_increment( int * pw ) 70 { 71 // int rv = *pw; 72 // if( rv != 0 ) ++*pw; 73 // return rv; 74 75 int rv, tmp; 76 77 __asm__ 78 ( 79 "movl %0, %%eax\n\t" 80 "0:\n\t" 81 "test %%eax, %%eax\n\t" 82 "je 1f\n\t" 83 "movl %%eax, %2\n\t" 84 "incl %2\n\t" 85 "lock\n\t" 86 "cmpxchgl %2, %0\n\t" 87 "jne 0b\n\t" 88 "1:": 89 "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2) 90 "m"( *pw ): // input (%3) 91 "cc" // clobbers 92 ); 93 94 return rv; 95 }

    The line 92 is assembly code. I really don't know what that means.

    I always do the check if the returned boost::weakptr<RtmpConnection>.lock() (type of boost::shared_ptr<RtmpConnection> is empty before I used it.

    So I googled, I saw this wiki.inkscape/wiki/index.php/Boost_shared_pointers

    Weak pointers cannot be dereferenced for thread safety reasons. If some other thread destroyed the object after you checked the weak pointer for expiry but before you used it, you would get a crash

  • So what should I do to deal with it, why does it crash (it seems boost::weakptr<RtmpConnection>.lock() should never crash)?
  • Since my program is multithreaded. It's possible that after I get and check the returned value of boost::weakptr<RtmpConnection>.lock(), the RtmpConnection might be destroyed by other thread, does the Boost Library guarantee that it won't be destroyed, because the return type is boost::shared_ptr<RtmpConnection>?
  • 解决方案

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

  • 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.

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

  • 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.)

    更多推荐

    提高:: weak

    本文发布于:2023-10-09 05:53:58,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1474888.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:weak

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!