[ace-users] regcomp, regexec, regfree Problems on Linux

jhoffert at dre.vanderbilt.edu jhoffert at dre.vanderbilt.edu
Wed Sep 26 22:33:42 CDT 2007

Hi, Doug.

> Hi Joe,
>    Could you post a small, self-contained example that illustrates the
> problem so that people could try this on other versions of UNIX/Linux?

Sure. I've attached the file.

I first tried a non-encapsulated test program with no objects but I didn't
have problems there. The problem seems to occur when I call regcomp () in
the constructor, regexec () in the operator ()() function overload method,
and regfree () in the destructor as illustrated in the attached file.
valgrind reports memory problems and I get a segmentation fault. If I
comment out the call to regfree then there's no segmentation fault.

Thanks for the help.


> thanks,
>         Doug
>>I'm using regcomp, regexec, and regfree on Linux (i.e., Linux
>> #1 SMP Thu Aug 30 13:47:21 EDT 2007 i686 i686 i386
>>GNU/Linux) and I've run into memory leak problems. Doug Schmidt thought
>>that these problems had been seen before and suggested I post to this
>>I originally just called regcomp once, regexec as many times as needed,
>>and then regfree once. However, valgrind tells me there are memory
>>problems. The only way I saw to fix the memory problems was to call
>>regcomp, regexec, and regfree consecutively (which defeats the purpose
>>of having regcomp).
>>Does anyone have any information on the problem I'm seeing?
>>Thanks for your help.
> --
> Dr. Douglas C. Schmidt                       Professor and Associate Chair
> Electrical Engineering and Computer Science  TEL: (615) 343-8197
> Vanderbilt University                        WEB:
> www.dre.vanderbilt.edu/~schmidt
> Nashville, TN 37203                          NET: d.schmidt at vanderbilt.edu
-------------- next part --------------
#include <sys/types.h>
#include <regex.h>

#include <iostream>
#include <set>
#include <string>
#include <iterator>

class RegexMatcher
  /// Ctor taking the regular expression pattern
  RegexMatcher (const std::string &pattern);

  /// Dtor to cleanup regex resources
  ~RegexMatcher ();

  /// Functor function called to determine if a word in
  /// the dictionary matches the regular expression pattern
  bool operator () (const std::string& word);

  /// The compiled regular expression.
  regex_t regexp_;

/// Ctor taking the regular expression pattern
RegexMatcher::RegexMatcher (const std::string &pattern)
  // Set up the regular expression processing.
  int retval = ::regcomp (&this->regexp_,
                          pattern.c_str (),
  if (retval != 0)
    throw std::exception ();

/// Dtor to clean up regex resources.
RegexMatcher::~RegexMatcher ()
  // Free up memory from the compiled regular expression.

/// Functor function called to determine if a word in
/// the dictionary matches the regular expression pattern
RegexMatcher::operator () (const std::string &word)
  // Check the word given the regular expression.
  int retval = ::regexec (&this->regexp_,
                          word.c_str (),

  return retval != 0 ? true : false;

main (int argc, char *argv[])
  // Set up the regular expression processing.
  RegexMatcher matcher(".*n.*b.*k.*");
  std::set<std::string> temp_set;
  std::set<std::string> word_set;

  word_set.insert ("minibike");

  std::remove_copy_if (std::set<std::string>::iterator (word_set.begin()),
                       std::set<std::string>::iterator (word_set.end ()),
                       std::inserter (temp_set,
                                      temp_set.begin ()),

  std::copy (temp_set.begin (),
             temp_set.end (),
             std::ostream_iterator<std::string> (std::cout,

  std::cout << "All done" << std::endl;

  return 0;

  // If RegexMatcher dtor calls ::regfree() then there are memory
  // problems and a segmentation fault when the matcher object goes
  // out of scope.

More information about the Ace-users mailing list