Browse Source

Fixed memory leak (#29)

* Fixed memory leak

Example:
```
Framework BRL.Blitz
Import BRL.StandardIO
Import Text.RegEx

Local html:String = "<div><div><div><div>Testing1</div></div></div><span>Hello</span><script>Testing2</script></div>"

Repeat
	Local regex:TRegEx = TRegEx.Create("<script(.*?)>(.*?)</script>")
	regex.ReplaceAll(html, "")

	GCCollect()
Forever
```

* Fixed RegEx references

Example:
```
Framework BRL.Blitz
Import BRL.StandardIO
Import Text.RegEx

Local html:String = """
<div>
	<span style="color:red">Testing</span>
</div>
"""

Local regex:TRegEx = TRegEx.Create("(<[^>]+) style=~q.*?~q")

Print regex.Replace(html, "\1")
```

* Change Long to Size_T

* More Size_T changes

* Change back method returns to Int

* Return Int rather than Size_T

---------

Co-authored-by: Brucey <[email protected]>
Carl Husberg 1 năm trước cách đây
mục cha
commit
5ac139d803
1 tập tin đã thay đổi với 13 bổ sung5 xóa
  1. 13 5
      regex.mod/regex.bmx

+ 13 - 5
regex.mod/regex.bmx

@@ -107,7 +107,7 @@ Type TRegEx
 	' the length of the target string
 	Field targLength:Size_T
 	
-	Field lastEndPos:Int
+	Field lastEndPos:Size_T
 	
 	' pointer to the compiled expression
 	Field pcre:Byte Ptr
@@ -132,6 +132,10 @@ Type TRegEx
 			pcre2_match_data_free_16(matchPtr)
 		End If
 		
+		If pcre
+			MemFree(pcre)
+		EndIf
+		
 	End Method
 
 	Rem
@@ -238,8 +242,8 @@ Type TRegEx
 			sizeOffsets = pcre2_get_ovector_count_16(matchPtr)
 			offsets = pcre2_get_ovector_pointer_16(matchPtr)
 
-			Local replaceStr:String = replaceWith 
-			Local ofs:Int Ptr = offsets
+			Local replaceStr:String = replaceWith
+			Local ofs:Size_T Ptr = offsets
 			For Local i:Int = 0 Until result
 				Local idx:Int = i * 2
 				replaceStr = replaceStr.Replace( "\" + i, lastTarget[ofs[idx]..ofs[idx+1]])
@@ -414,6 +418,10 @@ Type TRegEx
 		MemFree(pat)
 		
 		If bptr Then
+			If pcre
+				MemFree(pcre)
+			EndIf
+			
 			pcre = bptr
 		Else
 			Local buffer:Short[256]
@@ -714,7 +722,7 @@ Type TRegExMatch
 			MemFree(n)
 			
 			If index >= 0 Then
-				Local offsets:Int Ptr = pcre2_get_ovector_pointer_16(matchPtr)
+				Local offsets:Size_T Ptr = pcre2_get_ovector_pointer_16(matchPtr)
 				Return offsets[index]
 			End If
 		End If
@@ -738,7 +746,7 @@ Type TRegExMatch
 			MemFree(n)
 			
 			If index >= 0 Then
-				Local offsets:Int Ptr = pcre2_get_ovector_pointer_16(matchPtr)
+				Local offsets:Size_T Ptr = pcre2_get_ovector_pointer_16(matchPtr)
 				Return offsets[index + 1] - 1
 			End If
 		End If