C / C++ / MFC
|
|
 |

|
std::list<SMyFrameData*> m_FrameList;
CCriticalSection m_FrameListLock;
...
CTempLock tempLock(&m_FrameListLock);
if (m_FrameList.size() == 0) return 0L;
pFrameData = m_FrameList.front();
m_FrameList.pop_front();
... Decoder the frame data
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.
|
|
|
|

|
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.
|
|
|
|

|
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.
|
|
|
|

|
yes, I find that I copy wrong, now I changed it.
|
|
|
|

|
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.
|
|
|
|

|
The code you quoted contains that variable. You seen to be assuming it's a pointer.
Steve
|
|
|
|

|
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.
|
|
|
|

|
The definition is quoted in my post.
Steve
|
|
|
|

|
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.
|
|
|
|

|
It was in the original but isn't now as he edited his post after reading mine.
Steve
|
|
|
|
 |
|
|
General
News
Suggestion
Question
Bug
Answer
Joke
Rant
Admin