Skip to content

Fix mem leak in read_csv #57084

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

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions asv_bench/benchmarks/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ def time_read_stringcsv(self, engine):
def time_read_bytescsv(self, engine):
read_csv(self.data(self.BytesIO_input), engine=engine)

def peakmem_read_csv(self, engine):
read_csv(self.data(self.BytesIO_input), engine=engine)


class ReadCSVCategorical(BaseIO):
fname = "__test__.csv"
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/_libs/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ void parser_set_default_options(parser_t *self) {

parser_t *parser_new(void) { return (parser_t *)calloc(1, sizeof(parser_t)); }

static void parser_clear_data_buffers(parser_t *self) {
free_if_not_null((void *)&self->stream);
free_if_not_null((void *)&self->words);
free_if_not_null((void *)&self->word_starts);
free_if_not_null((void *)&self->line_start);
free_if_not_null((void *)&self->line_fields);
}

static void parser_cleanup(parser_t *self) {
// XXX where to put this
free_if_not_null((void *)&self->error_msg);
Expand All @@ -119,6 +127,7 @@ static void parser_cleanup(parser_t *self) {
self->skipset = NULL;
}

parser_clear_data_buffers(self);
if (self->cb_cleanup != NULL) {
self->cb_cleanup(self->source);
self->cb_cleanup = NULL;
Expand Down