Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Fix memory leak when searching regexp.
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 11, 2014
1 parent e436861 commit 4bb991f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/onig-reg-exp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ bool OnigRegExp::Contains(const string& value) {
return source_.find(value) != string::npos;
}

OnigResult* OnigRegExp::Search(const string& searchString, size_t position) {
if (!regex_)
shared_ptr<OnigResult> OnigRegExp::Search(const string& searchString,
size_t position) {
if (!regex_) {
ThrowException(Exception::Error(String::New("RegExp is not valid")));
return NULL;
}

int end = searchString.size();
OnigRegion* region = onig_region_new();
Expand All @@ -42,7 +45,7 @@ OnigResult* OnigRegExp::Search(const string& searchString, size_t position) {
ONIG_OPTION_NONE);

if (status != ONIG_MISMATCH) {
return new OnigResult(region, searchString);
return shared_ptr<OnigResult>(new OnigResult(region, searchString));
} else {
onig_region_free(region, 1);
return NULL;
Expand Down
4 changes: 3 additions & 1 deletion src/onig-reg-exp.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef SRC_ONIG_REG_EXP_H_
#define SRC_ONIG_REG_EXP_H_

#include <memory>
#include <string>

#include "oniguruma.h"

using ::std::shared_ptr;
using ::std::string;

class OnigResult;
Expand All @@ -16,7 +18,7 @@ class OnigRegExp {

bool Contains(const string& value);
int LocationAt(int index);
OnigResult *Search(const string &searchString, size_t position);
shared_ptr<OnigResult> Search(const string &searchString, size_t position);

private:
OnigRegExp(const OnigRegExp&); // Disallow copying
Expand Down
10 changes: 5 additions & 5 deletions src/onig-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,24 @@ Handle<Value> OnigScanner::FindNextMatch(Handle<String> v8String, Handle<Number>
OnigRegExp *regExp = (*iter).get();

bool useCachedResult = false;
OnigResult *result = NULL;
shared_ptr<OnigResult> result;

if (useCachedResults && index <= maxCachedIndex) {
result = cachedResults[index].get();
useCachedResult = (result == NULL || result->LocationAt(0) >= charOffset);
result = cachedResults[index];
useCachedResult = (!result || result->LocationAt(0) >= charOffset);
}

if (!useCachedResult) {
result = regExp->Search(string, byteOffset);
cachedResults[index] = shared_ptr<OnigResult>(result);
cachedResults[index] = result;
maxCachedIndex = index;
}

if (result && result->Count() > 0) {
int location = result->LocationAt(0);
if (bestIndex == -1 || location < bestLocation) {
bestLocation = location;
bestResult = result;
bestResult = result.get();
bestIndex = index;
}

Expand Down

1 comment on commit 4bb991f

@zcbenz
Copy link
Contributor Author

@zcbenz zcbenz commented on 4bb991f Mar 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found that the memory was actually not leaked, the pointer returned by OnigRegExp::Search was carefully saved to a shared_ptr.

Please sign in to comment.