[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.

-Joe

> thanks,
>
>         Doug
>
>>I'm using regcomp, regexec, and regfree on Linux (i.e., Linux
>>2.6.22.5-76.fc7 #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
>>group.
>>
>>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.
>>
>>-Joe
>>
>
>
> --
> 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
{
public:
  /// 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);

private:
  /// 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 (),
                          0);
  if (retval != 0)
    throw std::exception ();
}

/// Dtor to clean up regex resources.
RegexMatcher::~RegexMatcher ()
{
  // Free up memory from the compiled regular expression.
  ::regfree(&this->regexp_);
}

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

  return retval != 0 ? true : false;
}


int
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 ()),
                       matcher);

  std::copy (temp_set.begin (),
             temp_set.end (),
             std::ostream_iterator<std::string> (std::cout,
                                                 "\n"));

  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