Recent

Author Topic: Access Violation with TStringList  (Read 13297 times)

spitofland

  • New Member
  • *
  • Posts: 26
Access Violation with TStringList
« on: May 26, 2010, 11:50:42 am »
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?
« Last Edit: May 26, 2010, 11:52:38 am by spitofland »

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Access Violation with TStringList
« Reply #1 on: May 26, 2010, 12:06:59 pm »
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;
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

spitofland

  • New Member
  • *
  • Posts: 26
Re: Access Violation with TStringList
« Reply #2 on: May 26, 2010, 03:39:19 pm »
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

  • Hero Member
  • *****
  • Posts: 1646
Re: Access Violation with TStringList
« Reply #3 on: May 26, 2010, 03:54:02 pm »
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?
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;

<<edit>> Forgot to remove the var keyword...
« Last Edit: May 26, 2010, 04:03:26 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
Re: Access Violation with TStringList
« Reply #4 on: May 26, 2010, 04:41:01 pm »
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
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Access Violation with TStringList
« Reply #5 on: May 26, 2010, 04:50:36 pm »
don'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.
« Last Edit: May 26, 2010, 04:53:21 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

spitofland

  • New Member
  • *
  • Posts: 26
Re: Access Violation with TStringList
« Reply #6 on: May 27, 2010, 05:20:15 pm »
Thanks, it's helpful to know that.

 

TinyPortal © 2005-2018