Sign in to follow this  
Fuzztrek

STL: List strangeness

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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()
{
...
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this