Skip to content

Commit

Permalink
Fix memory leake with Regex operator (pytorch#2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayef211 committed Jan 19, 2023
1 parent 8c01462 commit 569d48d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions torchtext/csrc/regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions torchtext/csrc/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 569d48d

Please sign in to comment.