Commit 7abd42b
committed
vector_base: fix memory leak in reserve()
Currently, when using reserve() to actually grow the
storage in a vector, a new backend storage is allocated,
but the old one is never deallocated. A more minor unexpected
behavior that also occurred is that the vector would still apply
its exponential growth behavior, ie., it when taking a vector of
size 3 and calling `.reserve(4)` on it, one would actually end up
with a vector of size 6.
The logic to allocate the new storage, copy in the existing elements,
then destroy the old elements and swap in the new storage (which does
to the old storage being destructed and hence deallocated) is, essentially,
taken from how `.resize()` works. When growing a vector using `.reserve()`,
the new capacity will now be exactly what was specified in the call to
`.reserve()`.1 parent 1af5d90 commit 7abd42b
1 file changed
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
367 | | - | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
368 | 399 | | |
369 | 400 | | |
370 | 401 | | |
| |||
0 commit comments