* * *

Author Topic: [SOLVED] StringList Result from Function in two Units  (Read 480 times)

jasc2v8

  • New member
  • *
  • Posts: 36
[SOLVED] StringList Result from Function in two Units
« on: September 13, 2018, 07:49:37 pm »
Thanks for the help - the solution is:

Return StringList from Function:
Declare a StringList variable, but do not create before the Function.
The Function will create the StringList.
Free the StringList after use.

Return StringList from a Procedure:
Declare and create the StringList variable before the Procedure.
The Procedure will not create nor free this StringList.
Free the StringList after use.


I want to return a StringList from a function.
(I know, this is controversial, but assume it must be done)

All is good when the function is in one Unit.
However, when I have the same function in two Units I get a memory leak.
See my trivial example in the attached.

I'm sure I'm doing something wrong, any ideas?

I've researched and this is a good explanation of how to return StringList from a function;
http://forum.lazarus.freepascal.org/index.php/topic,33204.msg214727.html#msg214727

I do not want to do the alternate recommendation from the post above:
   bList:=Function(const aList: TStringList): TStringList;

I do not want to create a Class, just source code I can include in other projects.

My config: Win10 Home 64-bit, Lazarus 1.8.2 with FPC 3.0.4
« Last Edit: September 13, 2018, 08:43:30 pm by jasc2v8 »

Xor-el

  • Full Member
  • ***
  • Posts: 197
Re: StringList Result from Function in two Units
« Reply #1 on: September 13, 2018, 08:24:24 pm »
in unit1, delete line 67 (aList.Create;), you are creating it all wrong (and even if you doing it right), you are creating the stringlist multiple times.

Bart

  • Hero Member
  • *****
  • Posts: 3128
    • Bart en Mariska's Webstek
Re: StringList Result from Function in two Units
« Reply #2 on: September 13, 2018, 08:26:18 pm »
When you test both units, you assign the result of the function Test/TestUnit2 to the same variable.
Essentially doing:

Code: Pascal  [Select]
  1.   aList := TStringList.Create;
  2.   aList := TStringList.Create;
  3.   aList.Free;

Now the first block of memory for a TStringList is just dangling and cannot be accessed anymore, let alone be freed.

Also: whyis  the first line in the buttonclick
Code: Pascal  [Select]
  1.   aList.Create; << ???

Makes no sense to me whatsoever.

Bart

jasc2v8

  • New member
  • *
  • Posts: 36
Re: StringList Result from Function in two Units
« Reply #3 on: September 13, 2018, 08:39:22 pm »
Thanks!

Yes, the aList.Create is a total mistake trying to post while doing my day job.

I would never do all the creates and frees as in this example for real code.
However, you certainly cleared up my misunderstanding.
The attached now works so I updated as solved.
« Last Edit: September 14, 2018, 09:39:55 pm by jasc2v8 »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus