Click here to Skip to main content
       

C / C++ / MFC

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI am Bemused about std::list<>groupyu-jian13 Nov '12 - 4:17 
 
// Define 
std::list<SMyFrameData*>    m_FrameList;
CCriticalSection	m_FrameListLock;
... 
 
        //Pop the frame data from the font of list.
	CTempLock tempLock(&m_FrameListLock);
	if (m_FrameList.size() == 0) return 0L;
 
	pFrameData = m_FrameList.front();
	m_FrameList.pop_front();
        ... Decoder the frame data
 
                // use following code to push the frame data to the list.
              	SMyFrameData* pNewFrame = new SMyFrameData();
		pNewFrame->lpData = pBuf;
		pNewFrame->iSize	=	iSize;
 
		CTempLock (&(pProcess->m_FrameListLock));
		pProcess->m_FrameList.push_back(pNewFrame);
		SetEvent(pProcess->m_hFrameProcessEvent);
 
But now there is a error happen "erators and references can become invalid." in code, pFrameData = m_FrameList.front(); I just use the CCriticalSection to avoid the conflict, but why the error happen.
AnswerRe: I am Bemused about std::list<>mvpRichard MacCutchan13 Nov '12 - 5:37 
yu-jian wrote:
iterators and references can become invalid.
You are capturing a pointer to the first element in the list:
pFrameData = m_FrameList.front();
But later on you push an element into the list, so your pointer is no longer pointing to the first element in the list, and may therefore not be valid.
One of these days I'm going to think of a really clever signature.

GeneralRe: I am Bemused about std::list<> [modified]memberStephen Hewitt13 Nov '12 - 6:03 
Some of the code in question:
 std::list<SMyFrameData*> pFrameData; 
There's no capturing of pointers here, pFrameData is a std::list.
Only deleting a list node invalidates an iterator to it. Having an iterator to the first node then adding to the head is not a problem, you just end up with an iterator to the second item in the list (the same is true for pointers to list items).
Steve


modified 13 Nov '12 - 12:10.

GeneralRe: I am Bemused about std::list<>groupyu-jian13 Nov '12 - 6:25 
yes, I find that I copy wrong, now I changed it.
GeneralRe: I am Bemused about std::list<> [modified]mvpRichard MacCutchan13 Nov '12 - 6:38 
Stephen Hewitt wrote:
Some of the code in question:

 std::list<SMyFrameData*> pFrameData; 

Sorry, where does that come in OP's question?
 
Text changed for test purposes.
One of these days I'm going to think of a really clever signature.


modified 14 Nov '12 - 8:24.

GeneralRe: I am Bemused about std::list<>memberStephen Hewitt13 Nov '12 - 15:04 
The code you quoted contains that variable. You seen to be assuming it's a pointer.
Steve

GeneralRe: I am Bemused about std::list<> [modified]mvpRichard MacCutchan13 Nov '12 - 20:59 
Since the definition of that variable is not shown I assumed nothing.
 
[edit]
Testing the edit feature.
[/edit]
One of these days I'm going to think of a really clever signature.


modified 14 Nov '12 - 10:36.

GeneralRe: I am Bemused about std::list<>memberStephen Hewitt13 Nov '12 - 23:54 
The definition is quoted in my post.
Steve

GeneralRe: I am Bemused about std::list<>mvpRichard MacCutchan13 Nov '12 - 23:58 
But not in OP's original, which is why I added a comment to him/her suggesting that there was something else that we are not being told.
One of these days I'm going to think of a really clever signature.

GeneralRe: I am Bemused about std::list<>memberStephen Hewitt14 Nov '12 - 0:58 
It was in the original but isn't now as he edited his post after reading mine.
Steve

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid