changeset 3403:630fc934597f

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 07 Jun 2019 17:24:26 +0200
parents cf31b5bacce3
children e280ced38a4c
files Core/HttpServer/StringMatcher.cpp Core/HttpServer/StringMatcher.h
diffstat 2 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/StringMatcher.cpp	Fri Jun 07 15:06:17 2019 +0200
+++ b/Core/HttpServer/StringMatcher.cpp	Fri Jun 07 17:24:26 2019 +0200
@@ -51,6 +51,8 @@
     Algorithm algorithm_;
 
   public:
+    // WARNING - The lifetime of "pattern_" must be larger than
+    // "search_", as the latter internally keeps a pointer to "pattern" (*)
     Search(const std::string& pattern) :
       algorithm_(pattern.begin(), pattern.end())
     {
@@ -69,10 +71,14 @@
     
 
   StringMatcher::StringMatcher(const std::string& pattern) :
-    search_(new Search(pattern)),
     pattern_(pattern),
     valid_(false)
   {
+    // WARNING - Don't use "pattern" (local variable, will be
+    // destroyed once exiting the constructor) but "pattern_"
+    // (variable member, will last as long as the algorithm),
+    // otherwise lifetime is bad! (*)
+    search_.reset(new Search(pattern_));
   }
   
 
--- a/Core/HttpServer/StringMatcher.h	Fri Jun 07 15:06:17 2019 +0200
+++ b/Core/HttpServer/StringMatcher.h	Fri Jun 07 17:24:26 2019 +0200
@@ -48,8 +48,6 @@
   private:
     class Search;
       
-    // WARNING - The lifetime of "pattern_" must be larger than
-    // "search_", as the latter references "pattern_"
     boost::shared_ptr<Search>  search_;  // PImpl pattern
     std::string                pattern_;
     bool                       valid_;