Vector push_back Access Violation

纵饮孤独 提交于 2019-12-06 11:25:17

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.

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.

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

Apologies for being a time waster!

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.

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.

How are you calling TrackingSystem::addMarker()?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!