Hello all!
I was actually wondering if I should post this question in the Beginners section as it seems it's a simple problem, however I ultimately decided not to.
In a class of mine, I have the following declarations:
typedef list<Entity*> EntityList;
EntityList m_children;
In one of my member functions of the same class, I have this code:
void EntityImpl::DrawChildren()
{
EntityList::iterator begin = m_children.begin(); // For debug purposes
EntityList::reverse_iterator iter = m_children.rbegin();
EntityList::reverse_iterator rend = m_children.rend(); // For debug purposes
for (; iter != m_children.rend(); ++iter)
{
(*iter)->OnDraw();
}
}
If I try to run this code, really bad things happen. My computer has crashed with BSOD about 4 times now, with different error codes (BAD_POOL_CALLER and 2 other ones I can't remember). (Before anyone starts suggesting I check my RAM, harddrive and so on, let me assure you that's not the problem. My program is the problem. I have debugged a complete dump of my system with WinDBG, and the problem is a dereferenced pointer, that's invalid, inside my program. And that's exactly what is happening in the member function posted above)
Okay, if I debug my member function above, the variable "iter" is assigned the result of m_children.rbegin(). And this function returns an invalid pointer. (In debug mode, the iterator address returned is 0xcdcdcdcd) And basically, this is what I don't get. Is this really supposed to happen? Should I get an invalid pointer from this function?
When I'm debugging this, m_children has 1 item in the list. For debug purposes I added the "rend" variable which is assigned the m_children.rend() result. This function returns a VALID pointer, which is the 1 and only item in the list. The variable "begin" is also a valid pointer, pointing to the same element as "rend".
So when my program runs, it starts out with an invalid pointer, dereferences it and the program (And computer) crashes. I have been trying to figure out if I'm supposed to increment the pointer before I use it, but I can't believe that's the case, as I haven't been able to find any documentation that says that. Besides, I don't think that would be very intuitive... but I might be wrong...
So, alot of text and explainations. Does anyone have the silver bullet for me? What am I doing wrong here?
Thanks alot for any help!