EXC_BAD_ACCESS (code=1, address=0x50) on an apparently valid pointer

Hi,

I need help with this cryptic error I am having:

After some lib updates, I suddenly experience a segfault error in some of my executable products of my project.

The line of code crashing is simply in the likes of this:

shared_pointer<Object> p = make_shared<Object>(params);

// [ a lot of lines of code and different files later...]

coud << p << endl;

The pointer p appears valid, and lldb provides the following information of it:

p std::__1::shared_ptr<const wolf::StateBlock>::element_type @ 0x0000000102706998 strong=4 weak=2

and I can navigate through all the fields of the pointed Object (of type wolf::StateBlock).

I can "fix" this error with, for example, a cast of the pointer into an unsigned int:

shared_pointer<Object> p = make_shared<Object>(params);
[ a lot of lines of code and different files later...]
unsigned int pui = (unsigned int)p;
coud << pui << endl;

with the result of my code crashing somewhere else with a similar error.

Notes:

  • The error appears when I compile and execute the code in macOS Monterey.

  • The same code compiles and runs just fine in Ubuntu.

The error message is:

EXC_BAD_ACCESS (code=1, address=0x50)

The dumped assembler code reads:

7FF81EB1527C: 4C 89 E6                   movq   %r12, %rsi
7FF81EB1527F: 4C 89 EA                   movq   %r13, %rdx
7FF81EB15282: 4D 89 F0                   movq   %r14, %r8
7FF81EB15285: 41 FF 51 50                callq  *0x50(%r9)
7FF81EB15289: 48 85 C0                   testq  %rax, %rax
7FF81EB1528C: 75 17                      jne    0x7ff81eb152a5  ; <+261>
7FF81EB1528E: 48 8B 03                   movq   (%rbx), %rax

where the error is at the line 7FF81EB15285: 41 FF 51 50 callq *0x50(%r9)

Just to be a little more precise in the conditions for this error to show up. It relates to streaming into a std::ostream.

std::ostream str;

Eigen::Matrix3d M;
str << M; // this might fail

std::shared_ptr<Object> o(new Object());
str << o; // this might fail

Also, when I say "this might fail" is because it is not always failing, and depending on edits I can do to the code, it sometimes fails at trying to stream a shared_ptr (which is valid), sometimes an Eigen::Matrix (which is well defined).

the only thing I found consistent is that I always try to stream to std::ostream. If I stream to std::cout it works fine.

EXC_BAD_ACCESS (code=1, address=0x50) on an apparently valid pointer
 
 
Q