?
Solved

Access violation reading location on vector.push_back

Posted on 2010-08-24
9
Medium Priority
?
1,184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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
 
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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