Using Deque :: Iterator?

D

Donos

Hello

I have the following declaration in my code,

******************************************************************
struct mHandleObj
{
HANDLE h;
};

std::deque<mHandleObj> m_Hand;

std::deque<mHandleObj>::iterator m_Itr;
******************************************************************

Now in the actual implementation, there is...

******************************************************************

HANDLE hHand = ::CreateEvent(NULL,FALSE,FALSE,NULL);

mHandleObj* pObj = new mHandleObj;

pObj->h = hHand ;

m_Hand.push_back(*pObj );

This is where i push in data to Deque. After this i use the following
code to take data out of Deque.

m_Itr = m_Hand.begin();
mHandleObj& pWro = *m_Itr;

And then i do,

m_Hand.pop_back();
***********************************************************

This works fine in a few loops and after that, what happens is that at
some place the Deque breaks.
There is a memory exception and the debugger goes into some
"dbgheap.c" file which is associated with Visual Studio.

Is there anything wrong in this code?
 
V

Victor Bazarov

Donos said:
I have the following declaration in my code,

******************************************************************
struct mHandleObj
{
HANDLE h;
};

std::deque<mHandleObj> m_Hand;

std::deque<mHandleObj>::iterator m_Itr;
******************************************************************

Now in the actual implementation, there is...

******************************************************************

HANDLE hHand = ::CreateEvent(NULL,FALSE,FALSE,NULL);

Since 'CreateEvent' is not a standard function, we'll have to take
your word that it actually works and does what you need.
mHandleObj* pObj = new mHandleObj;

pObj->h = hHand ;

m_Hand.push_back(*pObj );

Seems like a waste of memory. Why do you need 'new'? Why can't you
simply do

mHandleObj m = { hHand };
m_Hand.push_back(m);

?
This is where i push in data to Deque. After this i use the following
code to take data out of Deque.

m_Itr = m_Hand.begin();
mHandleObj& pWro = *m_Itr;

And then i do,

m_Hand.pop_back();

pop_back does not take out the object pointed to by 'm_Hand.begin()'.
It takes out the _last_ element. Consider using 'pop_front'.
***********************************************************

This works fine in a few loops and after that, what happens is that at
some place the Deque breaks.

At what place? What do you mean by "breaks"?
There is a memory exception and the debugger goes into some
"dbgheap.c" file which is associated with Visual Studio.
OK.

Is there anything wrong in this code?

What code? The bits and pieces you cared to post seem fine (albeit
using 'new' and 'pop_back' inappropriately). Nothing can be said
about the rest of it, of course, since you decided to withhold it.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top