boost::make_shared 导致访问冲突
我有一个适用于 ARMV4I Windows Mobile 6 的 Visual Studio 2008 C++ 应用程序,其中我使用 boost::shared_ptr
来管理相当大的对象 (4KB)。不幸的是,boost::make_shared<>
会导致访问冲突异常。
我的代码:
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;
}
异常调用堆栈:
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++
异常的位置(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! */
针对 x86 Windows 进行编译时不会出现此问题。当像这样使用shared_ptr时,也不会出现此问题:
boost::shared_ptr< Foo > f1 = boost::shared_ptr< Foo >( new Foo );
任何人都可以解释发生了什么以及为什么这仅在ARMV4I Windows Mobile 6上出现问题?
谢谢, 保罗·H
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是对齐问题吧我不知道实现的细节,但
make_shared<>()
尝试将shared_ptr<>
对象和指向的对象分配在一个单一的对象中分配。这可能会导致两个对象之一最终到达的地址未按应有的方式对齐。这可以解释为什么它只在 ARM 上崩溃:该架构比普通 PC 硬件具有更严格的对齐要求。如果 int 或指针最终位于“奇怪”的地址,您的程序将在 ARM 上崩溃,而您的 PC 却可以愉快地访问数据。
Probably it's an alignment issue. I don't know the details of the implementation, but
make_shared<>()
tries to allocate theshared_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.
事实证明这是一个堆栈溢出问题。 ticket #4256 中已报告了这一情况。升级到提示可以修复该问题,因此应该会在下一个 Boost 更新中提供。
感谢 Peter Dimov 在 Boost 中用户ML。
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.