向量 push_back 访问冲突 [英] Vector push_back Access Violation

查看:54
本文介绍了向量 push_back 访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的错误,但它让我试图修复它发疯.

This is probably a silly error, but it's driving me nuts trying to fix it.

我有一个结构:

struct MarkerData
{  
 int pattId;
 unsigned short boneId;
 Ogre::Matrix4 transToBone;
 Ogre::Vector3 translation;
 Ogre::Quaternion orientation;

 MarkerData(int p_id, unsigned short b_id, Ogre::Matrix4 trans)
 {
  pattId = p_id;
  boneId = b_id;
  transToBone = trans;
 }
};

还有一个类:

class TrackingSystem
{
 public:
  void addMarker(int pattId, unsigned short boneId, Ogre::Matrix4 transToBone);

 private:  
  std::vector <MarkerData> mMarkers;
};

现在,在 addMarker 方法中:

Now, in the addMarker method:

    void TrackingSystem::addMarker(int pattId, unsigned short boneId, Ogre::Matrix4 transToBone)
{
    mMarkers.push_back(MarkerData(pattId,boneId,transToBone));
}

此 push_back 导致访问冲突OgreAR.exe 中 0x00471679 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000018.".

This push_back causes an access violation "Unhandled exception at 0x00471679 in OgreAR.exe: 0xC0000005: Access violation reading location 0x00000018.".

作为测试,我尝试了这个:

As a test, I tried this:

void TrackingSystem::addMarker(int pattId, unsigned short boneId, Ogre::Matrix4 transToBone)
    {
        std::vector <MarkerData> test;
        test.push_back(MarkerData(pattId,boneId,transToBone));
    }

这很好用.

我做错了什么?!谢谢!

What am I doing wrong?! Thanks!

推荐答案

您在其上调用 addMarkerTrackingSystem 对象已死的可能性很高(并且this 指针无效.要么超出范围、过早调用了删除,要么从未正确创建(指针仍为空).

Chances are high that the TrackingSystem object on which you're calling addMarker is dead (and that the this pointer is invalid. Either it went out of scope, delete was prematurely called, or it was never properly created (the pointer is still null).

这更有可能,因为 push_back 到本地向量工作正常.

This is even more likely since the push_back to a local vector works fine.

这篇关于向量 push_back 访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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