I am implementing two lists of class objects in my project. Originally, I had one vector container for one group of class objects and a list for the other but have just converted the vector implementation to a list.
Everything worked fine with the vector and list implementation, however, when I converted the vector to a list (and changed all the subsequent code) I get an unhandled exception when I try to push_back (or insert) an object to the list.
Unhandled exception at 0x004184e2 in PN_test.exe: 0xC0000005: Access violation reading location 0x00000004.
This results from the following:
In .hpp file:
class ssmcSection {
public:
data8* sectStartAddress;
data32 sectSize;
};
std::list<ssmcSection> sections;
In .cpp file:
ssmcSection sec0;
sec0.sectStartAddress = memHeadAddress;
sec0.sectSize = 0;
sections.push_back(sec0); //<- DIES IN THIS CALL
Exception in list library:
void _Insert(const_iterator _Where, const _Ty& _Val)
{ // insert _Val at _Where
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Where._Getcont() != this)
_DEBUG_ERROR("list insert iterator outside range");
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
_Nodeptr _Pnode = _Where._Mynode();
_Nodeptr _Newnode =
this->_Buynode(_Pnode, this->_Prevnode(_Pnode), _Val); // <- This is where the exception occurs in the list library
_Incsize(1);
this->_Prevnode(_Pnode) = _Newnode;
this->_Nextnode(this->_Prevnode(_Newnode)) = _Newnode;
}
EDIT:: SHOWING BEFORE AND AFTER
My Class Definition before:
class SMMC {
public:
...
class ssmcSection {
public:
data8* sectStartAddress;
data32 sectSize;
};
class smmcAllocData {
public:
bool allocated;
data8* start;
data8* end;
data32 sectionNum;
};
private:
std::list<smmcAllocData> memMap;
std::vector<ssmcSection> sections;
};
My class implementation before:
ssmcSection sec0;
sec0.sectStartAddress = memHeadAddress;
sec0.sectSize = 0;
sections.push_back(sec0);
...
smmcAllocData newSec;
newSec.allocated = true;
newSec.start = memHeadAddress;
newSec.end = memHeadAddress + spaceRequested;
newSec.sectionNum = sections.size()-1;
memMap.push_back(newSec);
All of which worked fine. Below shows the changes:
My Class Definition after:
class SMMC {
public:
...
class ssmcSection {
public:
data8* sectStartAddress;
data32 sectSize;
};
class smmcAllocData {
public:
bool allocated;
data8* start;
data8* end;
data32 sectionNum;
};
private:
std::list<smmcAllocData> memMap;
std::list<ssmcSection> sections; //changed from vector to list
};
My class implementation after:
ssmcSection sec0;
sec0.sectStartAddress = memHeadAddress;
sec0.sectSize = 0;
sections.push_back(sec0);
...
smmcAllocData newSec;
newSec.allocated = true;
newSec.start = memHeadAddress;
newSec.end = memHeadAddress + spaceRequested;
newSec.sectionNum = sections.size()-1;
memMap.push_back(newSec);
This fails at "sections.push_back(sec0);". Its the exact same thing that I was doing with the smmcAllocData list...!!??
I dont understand why it worked with the other list container, but not this one... All the examples for list show usage identical to this. I am using MS VS2010.
Any Thought?? Thanks!
Hmmm... It works now!?
I reverted back to the original implementation (using a vector and a list) and confirmed that it worked. I then commented out all the vector code and slowly replaced it with list (routinely running it to make sure it was still working through out the transition) until I replaced all the vector code....
Now it all works just like it is supposed to. To make sure I was not going crazy, I diff'ed the "broken code" from my original post to the working code, AND ITS THE SAME!
Either this is some odd MS VS issue or I am missing something.
User contributions licensed under CC BY-SA 3.0