访问违反std :: function assignement使用lambdas [英] Access violation on std::function assignement using lambdas

查看:216
本文介绍了访问违反std :: function assignement使用lambdas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家,这里再次。继续我上一个问题的代码:这是一个坏的黑客吗? memcpy与虚拟类
我纠正,使用克隆方法建议,但我有一个错误,也发生在我尝试memcpy事情(上面的问题)之前。



我想要做的是创建一个lambda,捕获当前脚本并执行它,然后传递和存储在一个对象(触发器*),在成员InternalCallback 。

我在lambda分配中遇到访问冲突错误: http://imgur.com/OKLMJpa
只有在这个代码的第4次迭代时才会发生错误:

  if(CheckHR(EnginePTR-> ; iPhysics-> CreateFromFile(physicsPath,StartingTriggerID,trans,scale,-1,false,engPtr))== HR_Correct)
{
_Lua :: ScriptedEntity * newScript = EntityBase-> Clone ); // 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 ++;
}

这是Trigger的代码

  class Trigger:public PhysicObject 
{
public:
Trigger()
{
ActorDynamic = nullptr;
ActorStatic = nullptr;
InternalCallback = nullptr;
}
virtual HRESULT更新(float ElapsedTime,void * EnginePTR);
virtual HRESULT Cleanup(); //释放演员!

long Id;
ShapeTypes类型;
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)>内部回
};

通过迭代,我的意思是作为一个for循环的一部分。
我的系统是Win 7 64位,Intel i3,NVIDIA GTX 480和编译器Visual Studio 2012 Express,使用C ++ 11工具集。
我真的没有想法。我测试了堆腐败,它似乎是好的,我改变了捕获在lambda,改变什么,我跳过第4个对象,它的工作原理。
任何帮助将非常感激。



编辑:根据需要,这里是callstack: http://imgur.com/P7P3t4k

解决方案

已解决。这是一个设计错误。我在地图中存储了很多对象,它们都是从一个对象类派生出来的(像上面一样,Trigger从PhysicObject派生)。
问题是,我有ID冲突,所以存储在ID 5中的对象不是一个触发器,所以铸造创建了一个坏的对象,所以程序崩溃。



愚蠢的错误,真的具体,但它可能有助于某人记住检查时态对象。


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).

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++;
}

This is the code for Trigger

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;
    };

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.

Edit: As required, here is the callstack: http://imgur.com/P7P3t4k

解决方案

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.

这篇关于访问违反std :: function assignement使用lambdas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆