Skip to content

[BUG] Fix memory leak with Regex C++ operator #2024

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 1 commit into from
Jan 19, 2023
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
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