矢量push_back访问冲突

发布于 2024-09-26 04:50:00 字数 1170 浏览 10 评论 0原文

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

我有一个结构:

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 方法中:

    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。”。

作为测试,我尝试了以下方法:

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

效果很好。

我做错了什么?!谢谢!

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

I have a struct:

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

And a class:

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

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

Now, in the addMarker method:

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

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

This works fine.

What am I doing wrong?! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜味超标? 2024-10-03 04:50:00

您调用 addMarkerTrackingSystem 对象很可能已死亡(并且 this 指针无效。要么它消失了在范围内,delete 被过早调用,或者从未正确创建(指针仍然为空),

因为到本地向量的push_back工作正常。

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

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

知你几分 2024-10-03 04:50:00

如果您正在做类似

TrackingSystem* p = new TrackingSystem();
delete p; //or p = 0, or anything that makes p not point to the object anymore
p->AddMarker( 0, 0, Ogre::Matrix4() );

或更简单的

TrackingSystem* p;
p->AddMarker( 0, 0, Ogre::Matrix4() );

事情,通常可能会发生这种情况,顺便说一句,最好将第三个参数作为 const 引用传递,以避免不需要的副本。

this typically could happen if you're doing something like

TrackingSystem* p = new TrackingSystem();
delete p; //or p = 0, or anything that makes p not point to the object anymore
p->AddMarker( 0, 0, Ogre::Matrix4() );

or even simpler

TrackingSystem* p;
p->AddMarker( 0, 0, Ogre::Matrix4() );

btw it might be better to pass the third argument as a const reference to avoid unneeded copies.

递刀给你 2024-10-03 04:50:00

只是为了调试,尝试

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

然后使用调试器进入那里,看看发生了什么。可能是您在其他地方发生了内存损坏,并且当您进入此函数调用时,TrackingSystem 的此实例具有损坏的 mMarkers 向量。

Just for debugging, try

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

Then get in there with the debugger and see what is going on. It could be that you have memory corruption happening somewhere else, and when you get into this function call this instance of TrackingSystem has a corrupted mMarkers vector.

许你一世情深 2024-10-03 04:50:00

抱歉,原来我是个布偶。我的 TrackSystem 实例在调用 addMarker 之前未初始化。

抱歉浪费了时间!

Sorry, turns out I was being a muppet. My instance of TrackSystem was not initialised before calling addMarker.

Apologies for being a time waster!

静谧幽蓝 2024-10-03 04:50:00

这些只是根据所提供信息的猜测。

  1. MarkerData 构造函数不会初始化平移或方向,因此它们将被默认初始化。这样就足够了吗? (顺便说一句,在该 ctor 中使用成员初始化列表而不是赋值。并通过 const 引用而不是通过值传递该 Matrix4 对象。)

  2. ceretullis 有一个很好的建议。根据问题中发布的代码,您确实无法确定崩溃是否发生在构建临时 MarkerData 对象期间或在 Push_back 调用期间。

  3. 基于“访问冲突读取位置 0x00000018”。发生这种情况可能是因为某个地方取消引用了空指针。您没有显示足够的代码来查看该空指针可能在哪里,或者它可能在 Ogre 库中的某个位置。

  4. 您的测试表明存在与堆栈相关的问题。当您在堆栈上声明向量而不是作为成员变量时,它会更改堆栈帧的布局。创建的临时 MarkerData 也会在堆栈上创建。如果您所做的测试没有问题,则可能是由于堆栈布局的差异 - 也许 MarkerData 的复制构造函数中存在一些缓冲区溢出,在您的示例中会调用几次。< /p>

These are just guesses based on the information provided.

  1. The MarkerData ctor does not initialize translation or orientation, so they will be default initialized. Is that adequate? (By the way, use the member initialization list in that ctor instead of assignment. And pass that Matrix4 object by const reference instead of by value.)

  2. ceretullis has a good suggestion. With your code as posted in the question, you really can't be sure if the crash is happening during construction of the temporary MarkerData object or during the push_back call.

  3. Based on "Access violation reading location 0x00000018." That's probably happening because there is a null pointer being dereferenced somewhere. You haven't shown enough code to see where that null pointer could be, or perhaps it is somewhere in the Ogre library.

  4. Your test suggests that there is a problem related to the stack. When you declare the vector on the stack instead of as a member variable, it changes the layout of the stack frame. The temporary MarkerData that gets created is also created on the stack. If you don't have a problem with that test you did, it's probably because of the difference in stack layout -- perhaps there is some buffer overflow in the copy constructor of MarkerData, which would be called a few times in your example.

咋地 2024-10-03 04:50:00

您如何调用 TrackingSystem::addMarker()

有可能是通过 NULL(或其他伪造的)TrackingSystem* 实现的吗?

How are you calling TrackingSystem::addMarker()?

By any chance is it through a NULL (or otherwise bogus) TrackingSystem*?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文