Access violation reading location on vector.push_back

Posted on 2010-08-24
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

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


	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

Open in new window

When I check where the error actually occurs it's here in xmemory:
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'/>
	<anim name='Bip01 R Thigh'>
		<time value='5'>
			<transform rot='-0.0953748 1.2315 3.08786 ' pivot='-0.0587772 -0.0409226 0.652361'/>

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.
Question by:Snapples
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
LVL 53

Expert Comment

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.
LVL 31

Expert Comment

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?


Author Comment

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;
    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)) {
                "%s at line %d\n",
            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)
            else if (wcscmp(atts[i],L"endFrame")==0)
            else if (wcscmp(atts[i],L"name")==0)
        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 )
        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)
        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)
            else if (wcscmp(atts[i],L"pivot")==0)
            else if (wcscmp(atts[i],L"scale")==0)
        float x, y,z,w;
        //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

Technology Partners: 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

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?
LVL 53

Expert Comment

ID: 33510166
You might have missed my response, Snapples ?

Author Comment

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.
LVL 53

Accepted Solution

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.

Author Comment

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.
LVL 53

Expert Comment

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 ! :)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 80
Dynamically allocate memory 9 70
Android development question 2 81
FMX TEdit KeyUp handler detecting  "enter" key 4 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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