Recent

Author Topic: {SOVED} how to check to see if TStringList has been created?  (Read 19811 times)

Michl

  • Full Member
  • ***
  • Posts: 226
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #15 on: February 14, 2014, 10:16:53 am »
Offtopic: @mangakissa, you can left away your outer exception block, it has no function.

If you want you can play with this (no Debugger but enabled Heaptrc):
Code: [Select]
function Test: TStringList;
begin
  Result:=TStringList.Create;
//  raise Exception.Create('TestException');
end;

procedure TForm1.Button1Click(Sender: TObject);
var FileList : TStringList;
begin
//  try
    try
      FileList := Test;
      ShowMessage(FileList.Text);
    except
      on E: Exception do
        ShowMessage('Exception: '+E.Message);
    end;
//  finally
    FileList.Free;
    ShowMessage('FileList.Free');
//  end;
end;   
The result is the same, with and without the outer exception block.
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

chain_reaction

  • New Member
  • *
  • Posts: 22
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #16 on: February 14, 2014, 11:41:18 am »
I don't like the current function :
Code: [Select]
Fileutil.FindAllFiles().
Most programmers included me, often forgot to deal with the instance created. Should it be changed to procedure?.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #17 on: February 14, 2014, 12:07:15 pm »
I haven't seen the function itself, but it's nothing more then using findfirst, findnext,findclose.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #18 on: February 14, 2014, 12:21:12 pm »
Quote from: Nichl
If you want you can play with this (no Debugger but enabled Heaptrc):
It could be, but how many people uses Heaptrc?
Does Lazarus/Delphi not using smart compiling?
I know Lazarus tells the developer id some code is useful or not.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #19 on: February 14, 2014, 04:04:15 pm »
Quote
I don't like the current function :
...
Most programmers included me, often forgot to deal with the instance created. Should it be changed to procedure?.
This function has existed for quite a long time, and many programs have used it. I don't think it should be changed just because you often forget to free the created instance. You have heaptrace to help you catch dangling pointers, combined with lineinfo in can show exact location of the unfreed memory. Changing a routine signature is not something that could be done "just like that", especially in a big projects like this. FYI, sockets interfaces gets deprecated for 2-3 stable versions before obsoleted.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 336
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #20 on: January 06, 2020, 06:07:48 pm »
Hy,
It seems that nobody answer to the first question: how do we know wether a StringList has been created or not. When we define a TStringList we can see that the variable is not Nil (as I see with the debuging). Has anybody a solution to this question ?
Windows 11 UTF8-64 - Lazarus 4.4-64 - FPC 3.2.2

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #21 on: January 06, 2020, 06:19:00 pm »
I fail to see your problem. If the variable doesn't contain nil (i.e. qqq<>nil or Assigned(qqq)) then an object was created and must be freed.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #22 on: January 06, 2020, 07:42:03 pm »
And if you want to be absolutely sure that Assigned(MyStringList) returns the "correct" result you can always assign it Nil somewhere, like in the constructor of the container object. For example (adapt to your needs):

Code: Pascal  [Select][+][-]
  1. type
  2.   TSomeObject = class
  3.     FSomeStrings: TStringList;
  4.     constructor Create;
  5.     destructor Destroy; override;
  6.     procedure GetMeSomeStrings;
  7.   end;
  8.  
  9. [...]
  10.  
  11. constructor TSomeObject.Create;
  12. begin
  13.   inherited Create;
  14.   FSomeStrings := Nil;
  15. end;
  16.  
  17. destructor TSomeObject.Destroy;
  18. begin
  19.   if Assigned(FSomeStrings) then
  20.     FSomeStrings.Free;
  21.   inherited Destroy;
  22. end;
  23.  
  24. procedure TSomeObject.GetMeSomeStrings;
  25. begin
  26.   if Assigned(FSomeStrings) then
  27.     FSomeStrings.Free;
  28.   FSomeStrings := FindAllFiles(...);
  29. end;
« Last Edit: January 06, 2020, 07:44:41 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 336
Re: {SOVED} how to check to see if TStringList has been created?
« Reply #23 on: January 06, 2020, 08:36:38 pm »
You are right !... That solves my problem. Thanks to everybody.
Windows 11 UTF8-64 - Lazarus 4.4-64 - FPC 3.2.2

 

TinyPortal © 2005-2018