使用 lambdas 对 std::function 赋值的访问冲突 [英] Access violation on std::function assignement using lambdas
问题描述
大家好,又来了.继续我上一个问题中的代码:这是一个糟糕的黑客吗?带有虚拟类的 memcpy我按照建议使用克隆方法纠正了这一点,但在尝试 memcpy 之前也发生了一个错误(阅读上面的问题).
Hy everyone, here again. Continuing the code from my previous question : Is this a bad hack? memcpy with virtual classes I corrected that, using the Clone approach as suggested, but I'm having an error that also happened before I tried the memcpy thing(read question above).
我想要做的是创建一个 lambda 来捕获当前脚本并执行它,然后将该 lambda 传递并存储在一个对象 (Trigger*) 中,在成员 InternalCallback 中.
我在 lambda 分配中收到访问冲突错误:http://imgur.com/OKLMJpa错误仅在此代码的第 4 次迭代时发生:
What I'm trying to do is to create a lambda that captures the current script and executes it, and then pass and store that lambda in an object ( Trigger*), in the member InternalCallback.
I get an access violation error on the lambda assignment: http://imgur.com/OKLMJpa
The error happens only at the 4th iteration of this code:
if(CheckHR(EnginePTR->iPhysics->CreateFromFile(physicsPath,StartingTriggerID,trans,scale,-1,false,engPtr)) == HR_Correct)
{
_Lua::ScriptedEntity * newScript = EntityBase->Clone(vm);//nullptr;
string luaPath = transforms.next_sibling().next_sibling().first_attribute().as_string();
if(UseRelativePaths)
{
stringstream temp2;
temp2 << _Core::ExePath() << LuaSubfolder << "\" << luaPath;
luaPath = temp2.str();
}
newScript->CompileFile(luaPath.c_str());
newScript->EnginePTR_voidptr = engPtr;
auto callback = [=](_Physics::Trigger* trigger,PxTriggerPair* pairs, PxU32 count)
{
newScript->SelectScriptFunction("TriggerCallback");
newScript->AddParam(trigger->Id);
auto data = (_Physics::RayCastingStats*)pairs->otherShape->userData;
newScript->AddParam((PxU8)pairs->flags);
newScript->AddParam(data->ID);
newScript->AddParam((int)data->Type);
newScript->AddParam((int)count);
newScript->Go(1);
return;
};
((_Physics::Trigger*)EnginePTR->iPhysics->GetPhysicObject(StartingTriggerID))->InternalCallback = callback;
StartingTriggerID++;
}
这是触发器的代码
class Trigger : public PhysicObject
{
public:
Trigger()
{
ActorDynamic = nullptr;
ActorStatic = nullptr;
InternalCallback = nullptr;
}
virtual HRESULT Update(float ElapsedTime,void * EnginePTR);
virtual HRESULT Cleanup(); // Release the actor!!
long Id;
ShapeTypes Type;
static const PhysicObjectType PhysicsType = PhysicObjectType::Trigger;
PxVec3 Scale;
void* UserData;
void Callback(PxTriggerPair* pairs,PxU32 count)
{
InternalCallback(this,pairs,count);
}
function<void(_Physics::Trigger* trigger,PxTriggerPair* pairs, PxU32 count)> InternalCallback;
};
通过迭代,我的意思是它是 for 循环的一部分.我的系统是 Win 7 64 位、Intel i3、NVIDIA GTX 480 和编译器 Visual Studio 2012 Express,使用 C++11 工具集.我真的没有想法.我测试了堆损坏,看起来不错,我更改了 lambda 中的捕获,什么也没改变,我跳过了第 4 个对象并且它可以工作.任何帮助将不胜感激.
By iteration I mean that is part of a for loop. My system is Win 7 64 bits, Intel i3, NVIDIA GTX 480, and the compiler Visual Studio 2012 Express, using the C++11 toolset. I'm really out of ideas. I tested for heap corruption, it appears to be good, I changed the capture in the lambda, changed nothing, I skip the 4th object and it works. Any help would be really appreciated.
根据要求,这是调用堆栈:http://imgur.com/P7P3t4k>
As required, here is the callstack: http://imgur.com/P7P3t4k
推荐答案
已解决.这是一个设计错误.我在地图中存储了很多对象,它们都派生自一个对象类(如上所述,其中 Trigger 派生自 PhysicObject ).问题是我遇到了 ID 冲突,因此存储在 ID 5 中的对象不是触发器,因此强制转换创建了一个坏对象,因此程序崩溃了.
Solved. It was a design error. I store a lot of objects in a map, and they all derive from an object class ( like above, where Trigger derives from PhysicObject ). The problem was that I was having IDs collisions, so the object stored in ID 5 wasn't a Trigger, so the cast created a bad object, and so the program crashed.
愚蠢的错误,非常具体,但它可能有助于某人记住检查时间对象.
Silly error, really specific, but it might help somebody to remember to check temporal objects.
这篇关于使用 lambdas 对 std::function 赋值的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!