469,631 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

List iterator assignment fails, assert iterator not dereferencable


Apologies for cross posing this but I am not sure if this is a VC 8 STL
bug or simply an invalid use of the iterator.

I have a PCI card access class which basically abstracts a third party
library from the higher level classes. This low level class is used to
allocate locked pages of memory before doing a DMA. The third party
library locks memory and returns a pointer to an internal structure,
which we can simply cast to a (void *).

My original intention was to have the low level PCI access class store
these third party pointers away in a list so that if the class is
destructed and the list is not empty (explicit unlocks have not been
performed) we can step through the list and tidy up nicely. The list is
defined in the class as:

// Some defines to wrap up the list/iterator type definitions.
typedef void * PCICardDMABufObject;
typedef list<PCICardDMABufObjectPCICardDMABufHandleList;
typedef PCICardDMABufHandleList::iterator PCICardDMABufHandle;

// List type in class.
PCICardDMABufHandleList *m_pDMAPageList;

The list is dynamically allocated in the constructor as:

m_pDMAPageList = new PCICardDMABufHandleList();

Along with storing the void pointers internally we also want to pass
these back to the higher level class which wraps up what the specific
PCI card can do, in this case it has a number of DMA channels. This
higher level class calls the low level classes lock function and
receives back an iterator of the void pointer stored in the low level
classes list as follows:

// List iterator
PCICardDMABufHandle hLockedBufHandle;

// Call to low level PCI class. Iterator returned in last parameter.
status = m_pPCICardInst->LockDMABuffer(pUserBufStartAddr, bytesToLock,
dmaDir, &pDMADescPageBufAddr[dmaDescPageBufIndex], &numDMAPages,

The function prototype in the low level class for this is:

PCIStatus LockDMABuffer(void *pDataBuf, unsigned int length,
PCICardDMADir dmaDir,
PCICardDMAPage *pDMAPages,
unsigned int *pNumPages,
PCICardDMABufHandle &hDMABufHandle);

Later on in the same function we assign the function we assign the list
iterator to a dynamically allocated array of list iterators for the
current DMA channel as follows:

// Store iterator away in higher level class for channel.
m_phUserDMABufHandles[m_numUserDMABufHandles] = hLockedBufHandle;

The array has a fixed max length defined by the largest DMA transfer.
The array is defined in the higher level class as:

PCICardDMABufHandle *m_phUserDMABufHandles;

and is allocated at construction with:

m_phUserDMABufHandles = new

The higher level class needs to keep track of which locked pages are
associated with a give DMA transfer.

This all works fine up to the point I benchmarked 4 DMA channels from 4
threads. I carefully protected the low level classes list access, both
read and write, with a mutex to prevent multiple threads updating the
list together and from some debugging output to stderr I am confident
that this is valid.

The problem I get is when I come to access the iterator stored in the
high level classes array of iterators once a DMA has completed and I
need to unlock all the buffers. The low level class is passed the
iterator back by the high level class, and it tries to dereference the
iterator to get the void pointer.

Now this is where the problem takes on an implementation specific
stance. The code asserts in VC 2005 with an "iterator not
dereferenceable" at:

if (this->_Mycont == 0
|| _Ptr == ((_Myt *)this->_Mycont)->_Myhead)
_DEBUG_ERROR("list iterator not dereferencable");

_Mycont is 0 when it fails.

This however is not where the problem actually starts, this is way back
when the buffer was originally locked and assigned to the array of
iterators m_phUserDMABufHandles. If I setup a conditional break point in
VC after the point of assignment at :

m_phUserDMABufHandles[m_numUserDMABufHandles] = hLockedBufHandle;

to test _Mycont of m_phUserDMABufHandles then after say 8000 iterations,
in 4 threads, the breakpoint fires as _Mycont is 0. The _Mycont of the
hLockedBufHandle however is valid, none 0. If I deliberately move the
point of execution back to the assignment and try again, the assignment
works and _Mycont gets a valid value.

So my problem is why does this not work? And why does it not work after
working for quite some considerable number of iterations previously?

Interestingly if I pass the
m_phUserDMABufHandles[m_numUserDMABufHandles] location directly to the
LockDMABuffer() call in place of the local hLockedBufHandle it seems to
work fine!

Am I doing something bad here with iterators? There seems to be few
references to what exactly you can and cannot do with an iterator. Most
references to "not dereferenceable" relate to already erased elements in
say vectors, this however is a list and from my understanding an
iterator for an element will remain valid regardless of adds or deletes
around it in the list. I can also see the element in the list in the
debugger so I know it is valid, it just seems to be the iterator which
is broken. It almost looks like a problem with the assignment of the

Any suggestions, help, sympathy much appreciated.

Oct 8 '07 #1
1 5904
David Bilsby wrote:
The code asserts in VC 2005 with an "iterator not
dereferenceable" at:

if (this->_Mycont == 0
|| _Ptr == ((_Myt *)this->_Mycont)->_Myhead)
_DEBUG_ERROR("list iterator not dereferencable");

_Mycont is 0 when it fails.
That basically means that either you didn't get this iterator from a
container or (maybe) that the container ceased to exist. Note that I'm not
sure with the latter part, it would require that a dying container zeroes
all the back pointers in its iterators and I'm not sure if the debug
variant of VC8's stdlib does that. However, that should be trivial to find

typedef ... container;

container::iterator it;
*it; // should fail, iterator not from container
container c(1);
it = c.begin();
*it; // okay, container still alive
*it; // should fail, container ceased to exist
Otherwise, can you distill a minimal example from your code?


Oct 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Santanu Chatterjee | last post: by
8 posts views Thread by freckred76 | last post: by
4 posts views Thread by Johan Pettersson | last post: by
61 posts views Thread by warint | last post: by
1 post views Thread by ashishbhatt12 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.