From 569d48dad5d9d14ed9eacb48b8aca0bf6c60ff96 Mon Sep 17 00:00:00 2001 From: Nayef Ahmed <22487263+Nayef211@users.noreply.github.com> Date: Wed, 18 Jan 2023 21:27:32 -0500 Subject: [PATCH] Fix memory leake with Regex operator (#2024) --- torchtext/csrc/regex.cpp | 4 ++++ torchtext/csrc/regex.h | 1 + 2 files changed, 5 insertions(+) diff --git a/torchtext/csrc/regex.cpp b/torchtext/csrc/regex.cpp index 65d20faec6..00ea624bdf 100644 --- a/torchtext/csrc/regex.cpp +++ b/torchtext/csrc/regex.cpp @@ -6,6 +6,10 @@ Regex::Regex(const std::string& re_str) : re_str_(re_str) { compiled_pattern_ = new RE2(re_str_); } +Regex::~Regex() { + delete compiled_pattern_; +} + std::string Regex::Sub(std::string str, const std::string& repl) const { RE2::GlobalReplace(&str, *compiled_pattern_, repl); return str; diff --git a/torchtext/csrc/regex.h b/torchtext/csrc/regex.h index 35e7e507f5..d868f94475 100644 --- a/torchtext/csrc/regex.h +++ b/torchtext/csrc/regex.h @@ -13,6 +13,7 @@ struct Regex : torch::CustomClassHolder { std::string re_str_; TORCHTEXT_API Regex(const std::string& re_str); + TORCHTEXT_API ~Regex(); TORCHTEXT_API std::string Sub(std::string str, const std::string& repl) const; TORCHTEXT_API bool FindAndConsume(re2::StringPiece* input, std::string* text) const;