Access Violation with TStringList lazarus / 31-LCL ---------------------------------------------------------------------- spitofland · 2010-05-26 09:50 I am running Lazarus-0.9.29-25159 and fpc-2.4.1-20100504 for Win 64-bit on a Windows 7 (64-bit) machine. I passed a TStringList object by reference to a function that I created. The function works perfectly, but inside the function I get an Access Violation if I try to run this code: Code: [Select]function TImportForm.ParseStr(Line:String;Delim,Quote:Char;var Data:TStringList) : Boolean; begin   Data.Clear;   //Access violation end;But I don't get any errors if I run: Code: [Select]function TImportForm.ParseStr(Line:String;Delim,Quote:Char;var Data:TStringList) : Boolean; begin   Data.Add('Test String');   Data.Add('Test String2');   Data.Add('Test String3'); end; Then, after using this function (without using the Clear function) I get an Access Violation when I call: Code: [Select]  ParseStr(CurrLine,ColumnChar,QuoteChar,Fields);   ShowMessage(Fields.Strings[1]); What would cause an Access Violation with Clear and Strings[1], but not with Add? ---------------------------------------------------------------------- eny · 2010-05-26 10:06 The AV is caused by passing a non-initialized object var. I'd say that that needs fixing. If you don't initialize the variable, it can contain anything and the results are unpredictable. BTW: there is no reason to pass the object as a ref parameter. Except when you really want to initialize it in your function, like so: Code: [Select]function TImportForm.ParseStr(Line:String;Delim,Quote:Char;var Data:TStringList) : Boolean; begin   Data := TStringList.Create;   Data.Add('Test String');   Data.Add('Test String2');   Data.Add('Test String3'); end; ---------------------------------------------------------------------- spitofland · 2010-05-26 13:39 Oh, thanks. I guess I should have caught that. When you say I don't have to pass it as a var, if I pass a TStringList as a normal parameter and make changes to it, will the changes persist? ---------------------------------------------------------------------- eny · 2010-05-26 13:54 Quote from: spitofland on May 26, 2010, 03:39:19 pmWhen you say I don't have to pass it as a var, if I pass a TStringList as a normal parameter and make changes to it, will the changes persist? You mean pass it as a value parameter: yes! As long as you initialize the objects before using them. Like this: Code: [Select]function TImportForm.ParseStr(Line:String; Delim, Quote:Char; Data:TStringList) : Boolean; begin   Data.Add('Test String');   Data.Add('Test String2');   Data.Add('Test String3');   result := true // or false... or whatever value you need... end; ..... var MyList: TStringList; begin   MyList := TStringList.Create;   if ParseStr('A String To Parse', ' ', '"', MyList) then ...   ... end; <> Forgot to remove the var keyword... ---------------------------------------------------------------------- mas steindorff · 2010-05-26 14:41 don't you need to use the var keyword in the funtion prameter list? function TImportForm.ParseStr(Line:String; Delim, Quote:Char; var Data:TStringList) : Boolean; or else you may be working on a copy of the varable/object ---------------------------------------------------------------------- eny · 2010-05-26 14:50 Quote from: mas steindorff on May 26, 2010, 04:41:01 pmdon't you need to use the var keyword in the funtion prameter list?No, hence my added comment. Objects are passed as a reference automatically. You are working on a copy of the object reference. But it's referring to the same, actual object. ---------------------------------------------------------------------- spitofland · 2010-05-27 15:20 Thanks, it's helpful to know that.