Skip to content

Commit 569d48d

Browse files
authored
Fix memory leake with Regex operator (#2024)
1 parent 8c01462 commit 569d48d

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

torchtext/csrc/regex.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Regex::Regex(const std::string& re_str) : re_str_(re_str) {
66
compiled_pattern_ = new RE2(re_str_);
77
}
88

9+
Regex::~Regex() {
10+
delete compiled_pattern_;
11+
}
12+
913
std::string Regex::Sub(std::string str, const std::string& repl) const {
1014
RE2::GlobalReplace(&str, *compiled_pattern_, repl);
1115
return str;

torchtext/csrc/regex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct Regex : torch::CustomClassHolder {
1313
std::string re_str_;
1414

1515
TORCHTEXT_API Regex(const std::string& re_str);
16+
TORCHTEXT_API ~Regex();
1617
TORCHTEXT_API std::string Sub(std::string str, const std::string& repl) const;
1718
TORCHTEXT_API bool FindAndConsume(re2::StringPiece* input, std::string* text)
1819
const;

0 commit comments

Comments
 (0)