user541686
user541686

Reputation: 210352

C++11 std::shared_ptr works fine, but boost::shared_ptr crashes, what can cause this?

I have a multithreaded program (can't reproduce it here -- it's thousands of lines) that works perfectly fine with C++11 (GCC 4.7.3) every single time, but which crashes when using C++03 (with Boost 1.53).

Specifically, I'm substituting std::atomic and std::shared_ptr with boost::atomic and boost::shared_ptr in my code (and compiling it with -std=c++03 in GCC). The code is otherwise the same.

However, the C++03/Boost version crashes or loops forever. When it crashes, it does so inside here:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff4dc2700 (LWP 4065)]
0x000000000043d0c8 in boost::detail::sp_counted_base::release() ()

The disassembly for this seems to be:

(gdb) disas 0x000000000043d0c8
Dump of assembler code for function _ZN5boost6detail15sp_counted_base7releaseEv:
   0x000000000043d0b0 <+ 0>:    mov    QWORD PTR [rsp-0x8],rbp
   0x000000000043d0b5 <+ 5>:    mov    ebp,0xffffffff
   0x000000000043d0ba <+10>:    mov    QWORD PTR [rsp-0x10],rbx
   0x000000000043d0bf <+15>:    mov    eax,ebp
   0x000000000043d0c1 <+17>:    sub    rsp,0x18
   0x000000000043d0c5 <+21>:    mov    rbx,rdi
=> 0x000000000043d0c8 <+24>:    lock xadd DWORD PTR [rdi+0x8],eax
   0x000000000043d0cd <+29>:    cmp    eax,0x1
   0x000000000043d0d0 <+32>:    je     0x43d0e8 <_ZN5boost6detail15sp_counted_base7releaseEv+56>
   0x000000000043d0d2 <+34>:    mov    rbx,QWORD PTR [rsp+0x8]
   0x000000000043d0d7 <+39>:    mov    rbp,QWORD PTR [rsp+0x10]
   0x000000000043d0dc <+44>:    add    rsp,0x18
   0x000000000043d0e0 <+48>:    ret    

I see rdi has the value 0x3f9dafee19598306, which looks wrong and is misaligned (the latter probably being the cause of the crash).

I've never seen any problems on the C++11 version (I've tested it both on Visual Studio 2013 on Windows and the current GCC on Linux), yet this happens every single time I run it on the C++03 version, so I doubt it's a race condition in my code, or I would've seen it in the C++11 version by now.

So I'm a little confused -- what difference between boost::shared_ptr and std::shared_ptr could possibly be causing this?


Update:

I'm using make_shared as well... and it seems like when I avoid using make_shared, the C++11 version infinite-loops on Linux too (still works fine on Windows). Not sure what that implies though.

Upvotes: 3

Views: 1188

Answers (1)

user541686
user541686

Reputation: 210352

I think I found the problem.

It was, in fact, not with boost::shared_ptr, but with boost::atomic elsewhere in my code.

boost::atomic does not automatically zero-initialize its value!

Upvotes: 2

Related Questions