
SGI STL STLPort deque memory leak (not related to optimized allocator)
Hi Everybody,
I found a memory leak in the deque template in SGI STL/STLPort recently.
It's unrelated to the optimized default allocator. Haven't found any
mention
of this on Deja/Google groups. So I'll post a note here because someone
is bound to come looking...
The behaviour of this bug is that anything you erase from the front of the
deque gets leaked. Have a look at SGI STL v3.3, stl_deque.h, in
deque<>::erase(iterator, iterator).
Lines 1013-1019 read as follows:
if (__elems_before < difference_type((this->size() - __n) / 2)) {
copy_backward(_M_start, __first, __last);
iterator __new_start = _M_start + __n;
destroy(_M_start, __new_start);
_M_destroy_nodes(__new_start._M_node, _M_start._M_node);
_M_start = __new_start;
}
Because __new_start comes after _M_start, line 1017 never destroys
any of the nodes. The fix is obvious:
_M_destry_nodes(_M_start._M_node, __new_start._M_node);
You'll see the same problem in STLPort 4.5 and likely previous versions
of SGI STL/STLPort.
Anyway, this fixed it for me, and deque now works perfectly as far as
I'm concerned. Good luck!
Qin Liu
[ about comp.lang.c++.moderated. First time posters: do this! ]