Access violation writing location 0xbaadf00d

Mar 20, 2011 at 4:35pm
Heya guys!

I'm going crazy here, been looking around at this error for hours now.
I have a class we can call Foo, if i use an std::vector<object*> in any way( .push_back() or .size() ) within this class I'll get this crash.

using a vector in the class that created Foo it works fine.
This is how the code looks:
1
2
3
4
CVertex* pVertex = new CVertex;
CMeshStructure* pMeshStruct = new CMeshStructure;
pMeshStruct.PushVertex(pMeshStruct); // crashes on the push_back in this function
// because _First within the vector class is 0xbaadf00d. 

tried:
1. If i create the vector outside my class cmeshstructure it all works fine.
2. If i make the vector hold objects instead of the address to object I don't get any crash.
3. I was thinking if it could be a buffer overflow(tho it should let me know) so i added a char pad[1024] before the vector declaration, still got the crash tho.

Any1 got any ideas? or do you need to see my CMeshStructure class declaration?
Mar 20, 2011 at 4:50pm
closed account (3hM2Nwbp)
Probably ought to post the CMeshStructure class. It could be that the vector was never initialised.
*Edit - I'm assuming that you're using Visual Studio. You can also step through the code with the debugger to see exactly where it's crashing.
Wikipedia wrote:

0xBAADF00D ("bad food") is used by Microsoft's LocalAlloc(LMEM_FIXED) to indicate uninitialised allocated heap memory when the debug heap is used.
Last edited on Mar 20, 2011 at 4:52pm
Mar 20, 2011 at 5:04pm
Thanks for a quick answer!

Yea it does indeed look like it's not initialised but I can't figure out why or what i can do about it.
the vector declaration inside CmeshStructure looks like this:
 
std::vector<CVertex*> m_Vertices;

So shouldn't it be initialized when i newed my CMeshStructure class?

If i go through the stack when it crashes I can see this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void push_back(const _Ty& _Val)
{	
     // insert element at end
     if (size() < capacity())

 #if _HAS_ITERATOR_DEBUGGING
     { // room at end, construct it there
	  _Orphan_range(_Mylast, _Mylast);
	  _Mylast = _Ufill(_Mylast, 1, _Val);
     }
 #else /* _HAS_ITERATOR_DEBUGGING */
     _Mylast = _Ufill(_Mylast, 1, _Val);    // <- THIS IS WHAT CAUSES THE CRASH
 #endif /* _HAS_ITERATOR_DEBUGGING */
     else
	   insert(end(), _Val);
}

Iterator debugging is not defined so the row:
_Mylast = _Ufill(_Mylast, 1, _Val);
gets executed, where _Mylast is 0xbaadf00d, causing the crash

BTW, the crash only happens in release and i don't use any memset or zeromemory which i know could otherwise cause this crash
Last edited on Mar 20, 2011 at 5:06pm
Mar 20, 2011 at 5:21pm
I have gone a bit further with the debugging and compared the vector that crashes with the same vector definition but outside of the class CMeshStructure.

The constructor for both vectors gets called as they should which in turn calls Buy(0).
which looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool _Buy(size_type _Capacity)
{	
	// allocate array with _Capacity elements
	_Myfirst = 0, _Mylast = 0, _Myend = 0;
	if (_Capacity == 0)
		return (false);
	else if (max_size() < _Capacity)
		_Xlen();	// result too long
	else
	{		// nonempty array, allocate storage
		_Myfirst = this->_Alval.allocate(_Capacity);
		_Mylast = _Myfirst;
		_Myend = _Myfirst + _Capacity;
	}
	return (true)
}


the difference is:
for the vector outside CMeshStructure:
before reaching this line _Myfirst, _Mylast and _Myend is all badfood.
_Myfirst = 0, _Mylast = 0, _Myend = 0;
after this line they are all 0

for the vector inside CMeshStructure:
before reaching this line _Myfirst, _Mylast and _Myend is all badfood.
_Myfirst = 0, _Mylast = 0, _Myend = 0;
after this they are all still badfood except for _Myend which is 0

I'm not experienced enough for this to tell me anything.. :/

Mar 20, 2011 at 5:31pm
closed account (3hM2Nwbp)
That seems odd...could you post the whole CMeshStructure class?

Never-mind that, if it works in debug, then your class should be fine. The only other thing that I can think of (improbable) would be that there could be other _Myfirst and _Mylast variables defined in a higher scope than just the vector class. I think the most likely cause would be some incorrect / incompatible setting(s) in Visual Studio dealing with release mode, or a binary incompatibility. Which runtime are you using? Multithread debug / multithreaded / multithreaded debug dll / multithreaded dll?
Last edited on Mar 20, 2011 at 5:38pm
Mar 20, 2011 at 5:55pm
OMG, you're a star Luc :D

My CMeshStructure class is wrapped in a reference counting template which uses T* m_pClass, same variable name as they use in vector, which messed everything up, thank you so much for you're time!
Mar 20, 2011 at 6:42pm
closed account (3hM2Nwbp)
This wouldn't have been an issue at all if the STL writers had qualified their member variables explicitly with the this pointer. That's something that I do 100% of the time to avoid issues like this. Anyhow, it's good to hear you have it solved.
Topic archived. No new replies allowed.