# ArrayList.toOwnedSlice: Fix potential for leaks when using errdefer · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/83e0e23f8aa2d90495bbf9c3649aa68f7c55c926)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 83e0e23f8aa2d90495bbf9c3649aa68f7c55c926

Requested commit: 83e0e23f8aa2d90495bbf9c3649aa68f7c55c926

Commit: 83e0e23f8aa2d90495bbf9c3649aa68f7c55c926

Tree: 47d4660de7134582454d3ef229fe6d516070208a

Author: Ryan Liptak

Committer: Andrew Kelley

## Message

```
ArrayList.toOwnedSlice: Fix potential for leaks when using errdefer

#13666 introduced a footgun when using `toOwnedSlice` with `errdefer array_list.deinit()`, since `toOwnedSlice` could retain capacity if `resize` failed, meaning it would leak without `deinit` being called. This meant that the only correct way to use `toOwnedSlice` was with `defer` instead of `errdefer` to ensure that the ArrayList would get cleaned up.

Now, toOwnedSlice will now behave similarly to how it did before #13666, in that it will always clear the ArrayList's capacity if the resize/realloc succeeds.

This also reverts commit 05890a12f532ba9d58904a14381ec174b9efe473, which was contingent on the modified toOwnedSlice behavior.

Closes #13946

```

## Parents

- [8ff9284c469226faa2be0c73ab27672e1e8a55d1](https://git.cafe/gitcafe/zig/commit/8ff9284c469226faa2be0c73ab27672e1e8a55d1?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/83e0e23f8aa2d90495bbf9c3649aa68f7c55c926?format=markdown)
