[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

Access violation reading location on vector.push_back

I'm reading some values from an xml file which I then put in a vector. However on certain values I get this error:
First-chance exception at 0x7568df2b in app.exe: 0xC0000005: Access violation reading location 0x6e617274

It always happens on the same values but that doesn't make any sense since there's nothing really different between those that work and those that don't.
I have a class AnimationNode which has a vector<TimedRotation>, which is simply a class that holds a few values (which are always initialized). I call a function to add the values in which I do the push_back and that is where the exception happens. All values enter my function correctly.


#pragma once
#include <vector>
using namespace std;
#include "TimedRotation.h"

typedef vector<TimedRotation>::const_iterator TRIT;

class AnimationNode
{
public:

	AnimationNode(tstring name);
	virtual ~AnimationNode(void);

	void AddTransformAt(int time, Float4 rotation,Float3 pivot);


	/**
	* The start of the rotations.
	*/
	TRIT TransformBegin(){return m_Rotations.begin();}
	/**
	* The end of the rotations.
	*/
	TRIT TransformEnd(){return m_Rotations.end();}

private:

	tstring m_Name;

	vector<TimedRotation> m_Rotations;

};


void AnimationNode::AddTransformAt(int time, Float4 rotation,Float3 pivot)
{
	TimedRotation tr(time,rotation,pivot,Float3(1,1,1));
	m_Rotations.push_back(tr);				// <-- ERROR HERE
	std::sort(m_Rotations.begin(),m_Rotations.end(),TimedRotationSorter());
}

Open in new window


When I check where the error actually occurs it's here in xmemory:
_STD_BEGIN
		// TEMPLATE FUNCTION _Allocate
template<class _Ty> inline
	_Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)
	{	// check for integer overflow
	if (_Count <= 0)
		_Count = 0;
	else if (((_SIZT)(-1) / _Count) < sizeof (_Ty))
		_THROW_NCEE(std::bad_alloc, NULL);

		// allocate storage for _Count elements of type _Ty
	return ((_Ty _FARQ *)::operator new(_Count * sizeof (_Ty)));      // <-- ERROR HERE
	}

Open in new window

With _Count having the value 52442120



I attached the xml file, for example, reading the values of Bip01 Spine works, those of Bip01 R Thigh cause the exception.
	<anim name='Bip01 Spine'>
		<time value='5'>
			<transform rot='-1.57185 -1.36542 -0.799044 ' pivot='0.0032068 -0.00310078 0.789961'/>
		</time>
	</anim>
	<anim name='Bip01 R Thigh'>
		<time value='5'>
			<transform rot='-0.0953748 1.2315 3.08786 ' pivot='-0.0587772 -0.0409226 0.652361'/>
		</time>
	</anim>

Open in new window



I don't quite get this, why do some values cause this while others don't?
Anyone who can shed some light on this?


Thanks in advance.
Idle.xml
0
Snapples
Asked:
Snapples
  • 4
  • 3
  • 2
1 Solution
 
Infinity08Commented:
>> When I check where the error actually occurs it's here in xmemory:

That sounds like a memory corruption. Check all of your code for buffer overflows etc.

>> With _Count having the value 52442120

That looks like a very high value. If that value is accurate, then that might be (part of) your problem - you're trying to store too much data in memory.

But I assume the value is not accurate, and that it's caused by the same memory corruption I suspect caused the crash.
0
 
ZoppoCommented:
Hi Snapples,

could you post the code where the XML is loaded and 'AnimationNode::AddTransformAt' is called? How do you create the 'AnimationNode's and how are they stored?

ZOPPO
0
 
SnapplesAuthor Commented:
I have the following classes.
TimedRotation, which holds a rotation, position and scale value
AnimationNode, which holds a vector of TimedRotation, 1 for each moment in time
Animation, which holds a map of AnimationNodes
AnimationReader, which reads the xml file and does the apropriate action at the start of each element.


[code]bool AnimationReader::Read()
{
    bool done = false;
    FILE* file;
    _wfopen_s(&file,GetFileName().c_str(),_T("r"));
    unsigned int length = _filelength(_fileno(file));
    char* buf= new char[length];
    do {
       
        size_t len = fread(buf, 1, length, file);
        done = len < length;
        if (XML_Parse(buf, len, done)) {
            fprintf(stderr,
                "%s at line %d\n",
                XML_ErrorString(XML_GetErrorCode()),
                XML_GetCurrentLineNumber());
            delete[] buf;
            return false;
        }
    } while (!done);
    delete[] buf;
    return true;
}

// At the start of each element
void AnimationReader::startElement(const XML_Char *name, const XML_Char **atts)
{
    if ( wcscmp(name,L"animation")==0)
    {
        tstring start,stop,animName;
        int i =0;
        while ( atts[i] != NULL )
        {
            if ( wcscmp(atts[i],L"startFrame")==0)
                start.assign(atts[i+1]);
            else if (wcscmp(atts[i],L"endFrame")==0)
                stop.assign(atts[i+1]);
            else if (wcscmp(atts[i],L"name")==0)
                animName.assign(atts[i+1]);
            i+=2;
        }
        m_pAnimation = new Animation(animName,ParseInt(start),ParseInt(stop));            // <-- <animation> creates a new Animation
    }
    else if (wcscmp(name,L"anim")==0)
    {
        tstring n;
        int i = 0;
        while (atts[i] != NULL )
        {
            if ( wcscmp(atts[i],L"name") == 0 )
                n.assign(atts[i+1]);
            i+=2;
        }
        m_pAnimationNode = new AnimationNode(n);
        m_pAnimation->AddAnimationNode(m_pAnimationNode);     // <-- <anim> Adds a new AnimationNode to the map, all are removed at the destructor of Animation
    }else if (wcscmp(name,L"time")==0)
    {
        tstring time;
        int i =0;
        while (atts[i] != NULL )
        {
            if (wcscmp(atts[i],L"value")==0)
                time.assign(atts[i+1]);
            i+=2;
        }
        m_CurrentTime =ParseInt(time);
    }else if (wcscmp(name,L"transform")==0)                    // <-- At the <transform> element in the xml
    {
        tstring quat,pivot,scale;
        int i =0;
        while (atts[i] != NULL)
        {
            //if (wcscmp(atts[i],L"quat")==0)
            //    quat.assign(atts[i+1]);
            if (wcscmp(atts[i],L"rot")==0)
                quat.assign(atts[i+1]);
            else if (wcscmp(atts[i],L"pivot")==0)
                pivot.assign(atts[i+1]);
            else if (wcscmp(atts[i],L"scale")==0)
                scale.assign(atts[i+1]);
            i+=2;
        }
        float x, y,z,w;
        w=0.0f;
        //swscanf_s(quat.c_str(),L"%f %f %f %f", &x , &y, &z ,&w);
        swscanf_s(quat.c_str(),L"%f %f %f", &x , &y, &z);
        float px,py,pz;
        swscanf_s(pivot.c_str(),L"%f %f %f",&px,&py,&pz);
       
        float sx,sy,sz;
        swscanf_s(scale.c_str(),L"%f %f %f",&sx,&sy,&sz);
        if (scale.size() == 0 )
            m_pAnimationNode->AddTransformAt(m_CurrentTime,Float4(x,y,z,w),Float3(px,py,pz));        // <-- AddTransformAt, all input values are correct
        else
            m_pAnimationNode->AddTransformAt(m_CurrentTime,Float4(x,y,z,w),Float3(px,py,pz),Float3(sx,sy,sz));
    }
}

[/code]
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ZoppoCommented:
hm - I do not really see a reason for the crash in that code - IMO the 'AnimationReader::m_pAnimationNode' somehow gets invalid while parsing the file (you told it starts reading fine but in between crashs, right?) but I can't see a place in the code where this can happen.

Maybe you should set a breakpoint in 'AnimationNode's destructor to see if it's deleted somewhere else ...

BTW, the XML file you attached, is it the same you're using for tests?
0
 
Infinity08Commented:
You might have missed my response, Snapples ?
0
 
SnapplesAuthor Commented:
Infinity, my apologies, I was still looking for any buffer overflows, I can't find any where this is the case though.

Zoppo, I put a breakpoint in the destructor, the only time they get deleted is when the destructor of Animation is called. I've tried using multiple xml files but there's always a few values where I get this exception.
0
 
Infinity08Commented:
Just to make sure it's not a memory size issue : are you actually storing millions of entries in the m_Rotations vector ?

If not, then a good place to start looking for memory corruption, is the memory in and around the vector.

For example, the m_Name data member, and the TimedRotation class.
0
 
SnapplesAuthor Commented:
Actually no. I'm only storing 1 entry per <time value> in the xml file, in the example xml I attached there's only one: 5. So each AnimationNode gets 1 entry in it's vector but even with other xml files it's almost never more than 30 entries per vector.

Anyway, you were right, there was a point where m_Name became null, nowhere near the vector though. I removed that and strangely enough the problem is gone. I don't quite get how that datamember can have any influence on the vector though.
0
 
Infinity08Commented:
>> I don't quite get how that datamember can have any influence on the vector though.

The m_Name data member is right next to the m_Rotations data member in memory, so if something is messed up with the first, that might cause problems with the second.

Glad you got it fixed ! :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now