From 95828f42a813c52aaa1534a3fcdbbfb0f9d850f5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 29 Jan 2016 16:40:38 -0800 Subject: [PATCH] Fix leak of StringInput wrapper struct --- src/runtime/document.c | 6 +++++- src/runtime/document.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/runtime/document.c b/src/runtime/document.c index b5416e56..34c7b19c 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -23,6 +23,7 @@ void ts_document_free(TSDocument *self) { ts_parser_destroy(&self->parser); if (self->tree) ts_tree_release(self->tree); + ts_document_set_input(self, (TSInput){}); ts_free(self); } @@ -49,12 +50,16 @@ TSInput ts_document_input(TSDocument *self) { } void ts_document_set_input(TSDocument *self, TSInput input) { + if (self->owns_input) + ts_free(self->input.payload); self->input = input; + self->owns_input = false; } void ts_document_set_input_string(TSDocument *self, const char *text) { ts_document_invalidate(self); ts_document_set_input(self, ts_string_input_make(text)); + self->owns_input = true; } void ts_document_edit(TSDocument *self, TSInputEdit edit) { @@ -82,7 +87,6 @@ int ts_document_parse(TSDocument *self) { if (!tree) return -1; - ts_tree_retain(tree); if (self->tree) ts_tree_release(self->tree); self->tree = tree; diff --git a/src/runtime/document.h b/src/runtime/document.h index 79b307b2..e23aad1f 100644 --- a/src/runtime/document.h +++ b/src/runtime/document.h @@ -12,6 +12,7 @@ struct TSDocument { TSTree *tree; size_t parse_count; bool valid; + bool owns_input; }; #endif