Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::make_shared causes access violation

I have a Visual Studio 2008 C++ application for ARMV4I Windows Mobile 6 where I'm using boost::shared_ptr<> to manage a fairly large object (4KB). Unfortunately, boost::make_shared<> causes an Access Violation exception.

My code:

struct Foo
{
    char a[ 4 * 1024 - 1 ];
};

int _tmain( int argc, _TCHAR* argv[] )
{
    boost::shared_ptr< Foo > f = boost::make_shared< Foo >(); // Access Violation
    return 0;
}

The exception callstack:

test.exe!boost::detail::sp_ms_deleter<o>::sp_ms_deleter<o>(void) Line: 60, Byte Offsets: 0x18   C++
test.exe!boost::make_shared<o>(void) Line: 106, Byte Offsets: 0x5c  C++
test.exe!wmain(int argc = 1, wchar_t** argv = 0x01b40060) Line: 81, Byte Offsets: 0x18  C++
test.exe!mainWCRTStartup(HINSTANCE__* hInstance = 0x00000003, HINSTANCE__* hInstancePrev = 0x00000000, unsigned short* lpszCmdLine = 0x00000003, int nCmdShow = 0) Line: 188, Byte Offsets: 0x94    C++

The location of the exception (boost\smart_ptr\make_shared.hpp):

template< class T > class sp_ms_deleter
{
    /* snip! */        
public:
    sp_ms_deleter(): initialized_( false )
    {    // line: 60  this = NULL
    }

    /* snip! */

This issue does not occur when compiling for x86 Windows. This issue also does not occur when using the shared_ptr like this:

boost::shared_ptr< Foo > f1 = boost::shared_ptr< Foo >( new Foo );

Can anybody explain what's going on and why this is breaking only on ARMV4I Windows Mobile 6?

Thanks, PaulH

like image 758
PaulH Avatar asked May 10 '11 19:05

PaulH


1 Answers

Probably it's an alignment issue. I don't know the details of the implementation, but make_shared<>() tries to allocate the shared_ptr<> object and the pointed-to object in one single allocation. Probably this causes one of the two objects to end up at an address that isn't aligned as it should be.

This would explain why it only crashes on ARM: That architecture has stricter alignment requirements than normal PC hardware. If an int or a pointer ends up on a "strange" address, your program will crash on ARM while your PC does happily access the data.

like image 161
sth Avatar answered Sep 29 '22 23:09

sth