【问题标题】:NVIDIA Unhandled exception at 0x002a2da2 in <work.exe>0xC0000005: Access violation reading location 0x00000000<work.exe> 0xC0000005 中 0x002a2da2 处的 NVIDIA 未处理异常:访问冲突读取位置 0x00000000
【发布时间】:2013-01-01 18:43:22
【问题描述】:

我目前正在尝试在一个角色上制作两条手臂并使用 NxRevoluteJoint 进行移动。我让这些在另一个作为示例给出的程序中完美运行,并且我在这个新项目中使用了相同的代码,但是我收到了一个错误(标题中的那个),我正在努力解决它。我知道指针在某个地方引用了 NULL,但我看不到如何对其进行排序。

变量是全局设置的:

NxRevoluteJoint* playerLeftJoint= 0;
NxRevoluteJoint* playerRightJoint= 0;

这是将播放器构建为复合对象的单独函数中的代码:

NxVec3 globalAnchor(0,1,0);     
NxVec3 globalAxis(0,0,1);       

playerLeftJoint= CreateRevoluteJoint(0,actor2,globalAnchor,globalAxis);
playerRightJoint= CreateRevoluteJoint(0,actor2,globalAnchor,globalAxis);


//set joint limits
NxJointLimitPairDesc limit1;
limit1.low.value = -0.3f;
limit1.high.value = 0.0f;
playerLeftJoint->setLimits(limit1);


NxJointLimitPairDesc limit2;
limit2.low.value = 0.0f;
limit2.high.value = 0.3f;
playerRightJoint->setLimits(limit2);    

NxMotorDesc motorDesc1;
motorDesc1.velTarget = 0.15;
motorDesc1.maxForce = 1000;
motorDesc1.freeSpin = true;
playerLeftJoint->setMotor(motorDesc1);

NxMotorDesc motorDesc2;
motorDesc2.velTarget = -0.15;
motorDesc2.maxForce = 1000;
motorDesc2.freeSpin = true;
playerRightJoint->setMotor(motorDesc2);

我收到错误的行是playerLeftJoint-&gt;setLimits(limit1);

【问题讨论】:

  • NxRevoluteJoint* playerLeftJoint= 0; 你正在取消引用一个空指针。
  • 如何才能不取消引用它?
  • 在使用之前使其指向现有的NxRevoluteJoint。右侧关节类似。
  • 好吧,我已经让它指向 NxRevoluteJoint CreateRevoluteJointNxRevoluteJoint 函数
  • 那一定是返回了NULL。该错误清楚地表明您正在尝试读取地址 0。

标签: c++ visual-studio-2010 nvidia


【解决方案1】:

CreateRevoluteJoint 返回一个空指针,就这么简单。错误消息非常清楚地表明指针的值为0。当然,你没有发布那个功能,所以这是我能给你的最好的信息。因此,这条线;

playerLeftJoint->setLimits(limit1);

取消引用指针playerLeftJoint,这是一个无效的指针。您需要初始化您的指针。我看不到你的整个程序结构,所以在这种情况下,最简单的解决方法是:

if(!playerLeftJoint)
    playerLeftJoint = new NxRevoluteJoint();

// same for the other pointer, now they are valid

此外,由于这是 C++ 而不是 C,请使用智能指针为您处理内存,即

#include <memory>

std::unique_ptr<NxRevoluteJoint> playerLeftJoint;

// or, if you have a custom deallocater...
std::unique_ptr<NxRevoluteJoint, RevoluteJointDeleter> playerLeftJoint;

// ...

playerLeftJoint.reset(new NxRevoluteJoint(...));

【讨论】:

  • 是否合理假设他使用的 API 中有一个自定义删除函数与分配函数 CreateRevoluteJoint 一起使用,如果是这样,智能指针的使用是否应该包含自定义删除器模板参数以确保调用正确的清理功能,因为它可能不是使用operator new()的简单堆分配?
  • @WhozCraig:可能。但是,这是一个论坛帖子,而不是一个包罗万象的解决方案。我不知道那是他的函数还是API函数。如果 OP 有帖子中未显示的其他限制,那么他应该能够使用这个答案并满足他的需求。不过这个建议不错,我会补充的。
  • 感谢@EdS 添加智能指针修复了我的工作,真的很感激。想我要出去做一些阅读以更好地理解这些内容。仅供将来参考,如果有人遇到此问题,if(!playerLeftJoint)playerLeftJoint = new NxRevoluteJoint(); 不起作用,因为它不是抽象类,但我确信它适用于标准 c++ 工作,因为它顶部有 Nvidia API
猜你喜欢
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 2014-10-26
  • 1970-01-01
相关资源
最近更新 更多