// Adapter info iterator
std::list<SD3DAdapterInfo>::iterator Adapter;
// t = 1
int t = m_Enumeration.GetAdapterInfo().size();
// This works fine.. test is equal to the info I populated the list with before
SD3DAdapterInfo test = m_Enumeration.GetAdapterInfo().front();
// This works fine.. test is still the same value
test = (*m_Enumeration.GetAdapterInfo().begin());
// Set iterator Adapter to the beginning
Adapter = m_Enumeration.GetAdapterInfo().begin();
// access the element from the iterator
//causes unhandled exception
// access violation reading location 0xfeeefef2.
test = (*Adapter);
STL: List strangeness
I'm writing a simple Direct3D enumerator that goes through all of the supported adapters, display modes, etc and places them in different lists.
Unfortunately, after I've populated them I seem to run into trouble. I guess it would just be easier to show the code:
As far as I can tell, accessing the actual element through the iterator is no different than *m_Enumeration.GetAdapterInfo().begin();, yet it causes an access violation. Anyone know what's going on?
Does GetAdapterInfo() return a std::list<SD3DAdapterInfo> by reference or by value? It should return it by reference, or else the returned object will be destroyed at the end of the line, and you'll have an iterator pointing to 'deleted' memory.
Good:
std::list<SD3DAdapterInfo>& GetAdapterInfo()
{
...
}
'Bad':
std::list<SD3DAdapterInfo> GetAdapterInfo()
{
...
}
Good:
std::list<SD3DAdapterInfo>& GetAdapterInfo()
{
...
}
'Bad':
std::list<SD3DAdapterInfo> GetAdapterInfo()
{
...
}
Quote:Original post by dalleboyAha! Thanks so much :D
Does GetAdapterInfo() return a std::list<SD3DAdapterInfo> by reference or by value? It should return it by reference, or else the returned object will be destroyed at the end of the line, and you'll have an iterator pointing to 'deleted' memory.
Good:
std::list<SD3DAdapterInfo>& GetAdapterInfo()
{
...
}
'Bad':
std::list<SD3DAdapterInfo> GetAdapterInfo()
{
...
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement