parse a delphi text , Access violation with function call -
I want to parse an ASCII text for all instances of the instance (bla, bla, bla), of the following code Using text in parenthesis
function filterText (const. Source, tagstart, tag and string): TstringList; Surcharge; var startups, endosse: integer; Start the result. Clear; Try StartPos: = PosEx (TagStart, Source); While StartPos & gt; Start Ink (Startups, Length (Todstart)); And Piece: = Points (tag, source, startup); result. Add (copy (source, startpiece, endosse - startup)); StartPos: = PosEx (TagStart, Source, and Peace + Length (Tagind)); End; Finally the end; End; I call this function using the following code
memo1.Lines.AddStrings (Filter text (memo1.Lines.Text, '( ',') ')); Rather than returning all the text between the brackets, I get an AV .... why ????
this line:
result. clear; Calls to an initial variable type of TStringList . It is undefined behavior in your case that appears as an access violation. You can instantiate the string list inside FilterText , and therefore the AV. Avoid Function FiltersTatch (...): TStringList; Start the result: = TStringList.Create; Try ... except for the result. free; Increased; End; End; But this is a big problem for you, who is taking ownership of the object? You use the function like this:
memo1.Lines.AddStrings (filtertext (memo1.Lines.Text, '(', ')')); Since FilterText is returning a new string list object in your design, nothing takes ownership and therefore you leak. You can do this:
filtered text: = filtertext (...); Try memo1.Lines.AddStrings (FilteredText); Filed text in the end. free; End; Since FilterText provides a new object immediately and the caller is expecting to take ownership, hence it is giving a name to indicate ownership transfers For example, you can call the function CreateFilteredText .
Another option expects the caller to instantiate the string list, and that object will be FilterText . I personally think a little easier to understand this approach. This version can look like this:
process filter text (..., fielded text: teestring); Start ... strings. Add (...); ... End; and from the collar:
filtered text: = TStringList.Create; Try FilterText (..., FieldText); Memo1.Lines.AddStrings (FilteredText); Finally filtered text. Free; End;
Comments
Post a Comment