简体   繁体   中英

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

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.

It turns out this is a stack overflow issue. This was already reported in ticket #4256 . Upgrading to the tips fixes it, so it should be available in the next Boost update.

Thanks to Peter Dimov in the Boost Users ML .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM