From 6d18003c048ed7ed98a026f1128f03fec4f9e60d Mon Sep 17 00:00:00 2001 From: Ethan Mahintorabi Date: Mon, 11 Aug 2025 16:25:47 -0700 Subject: [PATCH] util: Fix memory leak in PatternMatch (#282) When allocating a new string object in tcl you need increment and then decremenet the ref counter in order for objects to be correctly collected. Signed-off-by: Ethan Mahintorabi --- util/PatternMatch.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/PatternMatch.cc b/util/PatternMatch.cc index 94b32641..2dd32df9 100644 --- a/util/PatternMatch.cc +++ b/util/PatternMatch.cc @@ -89,7 +89,9 @@ PatternMatch::compileRegexp() anchored_pattern += '$'; Tcl_Obj *pattern_obj = Tcl_NewStringObj(anchored_pattern.c_str(), anchored_pattern.size()); + Tcl_IncrRefCount(pattern_obj); regexp_ = Tcl_GetRegExpFromObj(interp_, pattern_obj, flags); + Tcl_DecrRefCount(pattern_obj); if (regexp_ == nullptr && interp_) throw RegexpCompileError(pattern_); }