Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunk has memory leak #438

Closed
shoham-b opened this issue Apr 2, 2024 · 0 comments
Closed

Chunk has memory leak #438

shoham-b opened this issue Apr 2, 2024 · 0 comments

Comments

@shoham-b
Copy link

shoham-b commented Apr 2, 2024

Hi.
Appending to a one of the chunk resulted from lo.Chunk might leak into to consecutive chunk.

func TestChunk(t *testing.T) {
	allData := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	chunkedData := lo.Chunk(allData, 5)
	firstChunk := chunkedData[0]
	secondChunk := chunkedData[1]
	firstChunk = append(firstChunk, 666)
	fmt.Printf("secondChunk: %v\n", secondChunk) // secondChunk: [666 7 8 9 10]
}

The reason for that is that the slicing does not add the capacity, so the default max capacity is used, and then upon append the slice isn't reallocated.

To fix that we simply have to slice by 3 parts

		result = append(result, collection[i*size:last:last])

rather then the current implementation of

		result = append(result, collection[i*size:last]) // Bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants