Implement correct destruction

This commit is contained in:
Flaviu Tamas
2015-01-10 12:30:43 -05:00
parent 00b047a660
commit ca4cf24299
2 changed files with 17 additions and 9 deletions

View File

@@ -78,8 +78,14 @@ type
StudyError* = ref object of Exception
proc destroyRegex(self: Regex) =
pcre.free_substring(cast[cstring](self.pcreObj))
self.pcreObj = nil
if self.pcreExtra != nil:
pcre.free_study(self.pcreExtra)
proc initRegex*(pattern: string, options = "Sx"): Regex =
new result
new(result, destroyRegex)
result.pattern = pattern
var errorMsg: cstring
@@ -131,3 +137,6 @@ proc getNameToNumberTable(self: Regex): Table[string, int] =
idx += 1
result[name] = num
proc exec*(self: Regex, str: string): RegexMatch =
discard

View File

@@ -1,4 +1,3 @@
when not defined(pcreDll):
when hostOS == "windows":
const pcreDll = "pcre.dll"
@@ -10,7 +9,6 @@ when not defined(pcreDll):
else:
{.pragma: pcreImport, header: "<pcre.h>".}
#************************************************
# Perl-Compatible Regular Expressions *
#***********************************************
@@ -368,12 +366,13 @@ type
#that is triggered by the (?) regex item. For Virtual Pascal, these definitions
#have to take another form.
var malloc*: proc (a2: csize): pointer {.cdecl.}
var free*: proc (a2: pointer) {.cdecl.}
var stack_malloc*: proc (a2: csize): pointer {.cdecl.}
var stack_free*: proc (a2: pointer) {.cdecl.}
var callout*: proc (a2: ptr callout_block): cint {.cdecl.}
var stack_guard*: proc (): cint {.cdecl.}
{.emit: "#include <pcre.h>".}
proc malloc*(a2: csize): pointer {.cdecl, importc: "pcre_malloc", pcreImport.}
proc free*(a2: pointer) {.cdecl, importc: "pcre_free", pcreImport.}
proc stack_malloc*(a2: csize): pointer {.cdecl, importc: "pcre_stack_malloc", pcreImport.}
proc stack_free*(a2: pointer) {.cdecl, importc: "pcre_free", pcreImport.}
proc callout*(a2: ptr callout_block): cint {.cdecl, importc: "pcre_callout", pcreImport.}
proc stack_guard*(): cint {.cdecl, importc: "pcre_stack_guard", pcreImport.}
# User defined callback which provides a stack just before the match starts.