visual studio 2013 - x86/ c++ -指针指向指针:Const被编译器违反
visual studio 2013 - x86/C++ - Pointer To Pointer: Const being violated by compiler?
我正在为我的学生项目的游戏引擎执行一个共享指针(称为Handle),我们遇到了一个我们无法解释的漏洞。由于某种原因,在我们工厂的某一点上,有一个无效的内部指针通过句柄传递给工厂,这在我们的原型加载期间导致了崩溃。我们在整个过程中调试了几个小时,并将任何复杂的语句分解为最简单的版本,以便于调试。我最终将问题分离到Handle类的复制构造函数。然而,似乎仍然有一些中间步骤,其中内部指针被释放。我阅读了我能找到的所有关于导致这个问题的原因的文章,但没有找到任何东西。最后,我决定看一下拆卸,看看我是否能弄清楚发生了什么。下面是没有反汇编的复制构造函数。
template <class TYPE>
Handle<TYPE>::Handle(const Handle<TYPE>& rhs, bool weak) : m_weak(weak), m_ref_count(rhs.m_ref_count), m_ptr(rhs.m_ptr)
{
if(!m_weak)
++(*m_ref_count);
}
带反汇编的复制构造函数。
template <class TYPE>
Handle<TYPE>::Handle(const Handle<TYPE>& rhs, bool weak) : m_weak(weak), m_ref_count(rhs.m_ref_count), m_ptr(rhs.m_ptr)
{
013FFF50 push ebp
013FFF51 mov ebp,esp
013FFF53 sub esp,0CCh
013FFF59 push ebx
013FFF5A push esi
013FFF5B push edi
013FFF5C push ecx
013FFF5D lea edi,[ebp-0CCh]
013FFF63 mov ecx,33h
013FFF68 mov eax,0CCCCCCCCh
013FFF6D rep stos dword ptr es:[edi]
013FFF6F pop ecx
013FFF70 mov dword ptr [this],ecx
013FFF73 mov eax,dword ptr [this]
013FFF76 mov cl,byte ptr [weak]
013FFF79 mov byte ptr [eax],cl
013FFF7B mov eax,dword ptr [this]
013FFF7E mov ecx,dword ptr [rhs]
013FFF81 mov edx,dword ptr [ecx+4]
013FFF84 mov dword ptr [eax+4],edx
013FFF87 mov eax,dword ptr [this]
013FFF8A mov ecx,dword ptr [rhs]
013FFF8D mov edx,dword ptr [ecx+8]
013FFF90 mov dword ptr [eax+8],edx
if(!m_weak)
013FFF93 mov eax,dword ptr [this]
013FFF96 movzx ecx,byte ptr [eax]
013FFF99 test ecx,ecx
013FFF9B jne Handle<Component>::Handle<Component>+60h (013FFFB0h)
++(*m_ref_count);
013FFF9D mov eax,dword ptr [this]
013FFFA0 mov ecx,dword ptr [eax+4]
013FFFA3 mov edx,dword ptr [ecx]
013FFFA5 add edx,1
013FFFA8 mov eax,dword ptr [this]
013FFFAB mov ecx,dword ptr [eax+4]
013FFFAE mov dword ptr [ecx],edx
}
问题(据我所知)在这里:
013FFF6D rep stos dword ptr es:[edi]
众所周知,这是用来快速清除内存的。然而,这条指令清除了rhs Handle内部指针指向的指针,不仅如此,它还违反了const,因为Handle中传递的是一个const引用。我不确定这是否是一个VS问题,在我们的构建设置中设置的某种标志导致它发生,但是复制构造函数工作得很好,直到我们试图用它来制作原型。
任何帮助都将非常感激!
编辑1:我已经进一步研究了这个问题后,一些评论带来的光,我可能会返回一个临时变量在我的代码的某个地方。我没能找到任何迹象表明我在这么做,但我确实筛选了更多的拆解,看看是否能找到任何线索。这是我们的CreateGenericArchetype函数,这就是问题出现的地方。
Handle<Object> ObjectFactory::CreateGenericArchetype(const std::wstring &ArchetypeName)
{
/* Create The Object */
auto archetype = mComponentFactoryMap.find(std::wstring(L"Object"));
Handle<Component> created_component = archetype->second->CreateArchetypeComponent();
Handle<Object> retVal = static_handle_cast<Object>(created_component);
retVal->mArchetypeName = ArchetypeName;
mArchetypeMap.insert(std::pair<std::wstring, Handle<Object>>(ArchetypeName, retVal));
return Handle<Object>(retVal, true);
}
引起我们所有问题的行(目前)是:
Handle<Component> created_component = archetype->second->CreateArchetypeComponent();
当然,我也扩展了我的调试CreateArchetypeComponent,所以这里也是:
Handle<Component> ComponentTypeFactory<Object>::CreateArchetypeComponent() const
{
Object* created_object = new Object;
Component* casted_object = static_cast<Component*>(created_object);
Handle<Component> newComponent(casted_object);
newComponent->mType = mType;
newComponent->mID = GetNewID();
return newComponent;
}
Object继承自Component,所以Object的static_cast向下转换为Component是有效的,并且构造函数成功地从Component*构造了句柄。当我们试图返回在函数内部构造的Handle时,问题就出现了。下面是该交互的反汇编:
return newComponent;
005602AD push 0
005602AF lea eax,[newComponent]
005602B2 push eax
005602B3 mov ecx,dword ptr [ebp+8]
005602B6 call Handle<Component>::Handle<Component> (04EA050h)
005602BB mov ecx,dword ptr [ebp-110h]
005602C1 or ecx,1
005602C4 mov dword ptr [ebp-110h],ecx
005602CA mov byte ptr [ebp-4],0
005602CE lea ecx,[newComponent]
005602D1 call Handle<Component>::~Handle<Component> (04F2E7Bh)
005602D6 mov eax,dword ptr [ebp+8]
}
00EB02D9 push edx
00EB02DA mov ecx,ebp
00EB02DC push eax
00EB02DD lea edx,ds:[0EB0318h]
00EB02E3 call @_RTC_CheckStackVars@8 (0E4BA9Eh)
00EB02E8 pop eax
00EB02E9 pop edx
00EB02EA mov ecx,dword ptr [ebp-0Ch]
00EB02ED mov dword ptr fs:[0],ecx
00EB02F4 pop ecx
00EB02F5 pop edi
00EB02F6 pop esi
00EB02F7 pop ebx
00EB02F8 mov ecx,dword ptr [ebp-10h]
00EB02FB xor ecx,ebp
00EB02FD call @__security_check_cookie@4 (0E4E9BAh)
00EB0302 add esp,130h
00EB0308 cmp ebp,esp
00EB030A call __RTC_CheckEsp (0E41C3Dh)
00EB030F mov esp,ebp
00EB0311 pop ebp
00EB0312 ret 4
* * * * * * *回CreateGenericArchetype * * * * * * *
005C2639 call __RTC_CheckEsp (04F1C3Dh)
005C263E mov dword ptr [ebp-1D4h],eax
005C2644 mov ecx,dword ptr [ebp-1D4h]
005C264A mov dword ptr [ebp-1D8h],ecx
005C2650 mov byte ptr [ebp-4],4
005C2654 mov edx,dword ptr [ebp-1D8h]
005C265A push edx
005C265B lea ecx,[created_component]
005C265E call Handle<Component>::Handle<Component> (04EA050h)
*******复制构造函数从这里反汇编*******
005C2663 mov byte ptr [ebp-4],6
005C2667 lea ecx,[ebp-174h]
005C266D call Handle<Component>::~Handle<Component> (04F2E7Bh)
Handle<Object> retVal = static_handle_cast<Object>(created_component);
除非我的复制构造函数被称为"created_component"作为rhs句柄被认为返回一个局部变量,我看不出它可能会出错的地方,除非这是因为从CreateArchetypeComponent返回的句柄是在堆栈上,当它传递给复制构造函数,然后被清除。
谢谢你在这个问题上的大力帮助,但我们知道是怎么回事了。发生的事情是,在一些Handle构造函数中,我们需要为Handle的内部指针对指针将要指向的指针分配内存。所以发生的事情是句柄本身使用的是堆栈上的指针,当rep stos
操作码被调用时,它正在清除该指针。
- 在 const 函数中通过引用和指针返回之间的区别
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 使用共享指针的函数调用,其对象应为 const
- 为什么通过指针编译时不能分配 const 初始化
- 如果我在 const 函数上使用指针,我可以返回什么?
- 为什么在引用指针时将 const 放在 & 符号的左侧有效,而在右侧则无效?
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 为什么指针没有隐式转换为 <type> const*const
- 将const指针(EVP_MD)保存到变量中,以将其重新使用为类方法(HMAC)
- 为什么我可以更改指针的常数(const char *)槽的值
- 错误:"->"的基本操作数具有非指针类型"const"
- 无法将参数 3 从 'const QGraphicsItem *' 转换为 'const QObject *',这是指针,对象是 QGraphicsObject 的子类
- 为什么 map::找不到指向 const 的指针?
- std :: allocator_traits ::用const指针构造
- 如何为const char double指针(使用新的)动态分配内存
- 功能指针参数参数转换为const
- 了解const自动指针
- 从 'void *' 转换为 'const std::string *&' 必须具有所有中间指针 const 合格
- visual studio 2013 - x86/ c++ -指针指向指针:Const被编译器违反