STL: List strangeness

Started by
2 comments, last by Fuzztrek 19 years, 5 months ago
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:

// 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);
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?
Advertisement
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()
{
...
}
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Use debugger and step trought he code.
Quote:Original post by dalleboy
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()
{
...
}
Aha! Thanks so much :D

This topic is closed to new replies.

Advertisement