Solved

Access violation reading location on vector.push_back

Posted on 2010-08-24
9
1,119 Views
Last Modified: 2012-05-10
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
Comment
Question by:Snapples
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33509062
>> 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
 
LVL 30

Expert Comment

by:Zoppo
ID: 33509293
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
 
LVL 2

Author Comment

by:Snapples
ID: 33509829
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
 
LVL 30

Expert Comment

by:Zoppo
ID: 33509961
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 53

Expert Comment

by:Infinity08
ID: 33510166
You might have missed my response, Snapples ?
0
 
LVL 2

Author Comment

by:Snapples
ID: 33510279
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 33510458
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
 
LVL 2

Author Comment

by:Snapples
ID: 33512240
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33512500
>> 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now