Home
Top.Mail.Ru Yandeks.Metrika
Forum: "Beginners";
Current archive: 2010.08.27;
Download: [xml.tar.bz2];

Down

Question about releasing objects in an object Find similar branches


Девушка   (2010-04-05 14:08) [0]

There is a class in which TStringList needs to be declared as class fields.

It looks something like this
TRecordExtractor=class (TObject)

  private
    NowString:longint;
    Function GetInStr:TStringList;
    Procedure SetInStr(InStrP:TStringList);
    Procedure SetOutStr(OutStrP:TStringList);
    Function GetOutStr:TStringList;

  public
    Property InStr:TStringList read GetInStr write SetInStr; //входной стринглист
    Property OutStr:TstringList read GetOutStr write SetOutStr;//выходной стринглист (3 строчки или сколько получится)
    Constructor Create; overload;
    Destructor Destroy; overload;
  end;

Constructor TRecordExtractor.Create;
Begin
 Inherited;
 InStr:=TStringList.Create;
 OutStr:=TStringList.Create;
 NowString:=0;
End;

Destructor TRecordExtractor.Destroy;
Begin
 InStr:=TStringList.Free;
 OutStr:=TStringList.Free;
 Inherited;
End;

Function TRecordExtractor.GetInStr:TStringList;
Begin
GetInStr:=InStr;
End;

Function TRecordExtractor.GetOutStr:TStringList;
Begin
GetOutStr:=OutStr;
End;

Procedure TRecordExtractor.SetInStr(InStrP:TStringList);
Begin
InStr:=InStrP;
End;

Procedure TRecordExtractor.SetInStr(OutStrP:TStringList);
Begin
OutStr:=OutStrP;
End;

Question: when calling RecordExtractor.free, the described destructor will not be executed? How to correctly describe what would be executed and TStringLists freed?



Сергей М. ©   (2010-04-05 14:11) [1]

First of all, you should once and for all comprehend the difference between overload and override.
These are two different differences, and overload is absolutely out of place here, which cannot be said about override.



И. Павел ©   (2010-04-05 14:11) [2]

InStr:=TStringList.Free;
OutStr:=TStringList.Free;

:)
You need this:
InStr.Free;
OutStr.Free;



oldman ©   (2010-04-05 14:19) [3]

Did something happen at the conservatory?
Over the past week, it seems, the fifth thread on this topic.
And all with the same rake.
:)



Anatoly Podgoretsky ©   (2010-04-05 14:26) [4]

> oldman (05.04.2010 14: 19: 03) [3]

At the conservatory, it was time for laboratory and other work.
DM is their free artist.



Игорь Шевченко ©   (2010-04-05 14:27) [5]


> Destructor Destroy; overload;


What a ?

override need



Девушка   (2010-04-05 14:43) [6]


> I. Pavel © (05.04.10 14: 11) [2]

for sure ... but that's not the point ...


> Sergey M. © (05.04.10 14: 11) [1]
> First of all, you should once and for all comprehend the difference between
> overload and override.
> These are two different differences, and overload is absolutely out of place here,
> what can not be said about override.

Taken from here http://www.delphisources.ru/pages/faq/faq_delphi_basics/Constructor.php.html
but the error is understood

But when you try to specify override, it writes Cannot ovveride a static method ...
Compiles like this

 TRecordExtractor=class
  private
    NowString:longint;
    Function GetInStr:TStringList;
    Procedure SetInStr(InStrP:TStringList);
    Procedure SetOutStr(OutStrP:TStringList);
    Function GetOutStr:TStringList;

  public
    Property InStr:TStringList read GetInStr write SetInStr; //входной стринглист
    Property OutStr:TstringList read GetOutStr write SetOutStr;//выходной стринглист (3 строчки или сколько получится)
    Constructor Create;
    Destructor Destroy;
  end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

Constructor TRecordExtractor.Create;
Begin
 Inherited;
 InStr:=TStringList.Create;
 OutStr:=TStringList.Create;
 NowString:=0;
End;

Destructor TRecordExtractor.Destroy;
Begin
 InStr.free;
 OutStr.free;
 Inherited;
End;

Function TRecordExtractor.GetInStr:TStringList;
Begin
GetInStr:=InStr;
End;

Function TRecordExtractor.GetOutStr:TStringList;
Begin
GetOutStr:=OutStr;
End;

Procedure TRecordExtractor.SetInStr(InStrP:TStringList);
Begin
InStr:=InStrP;
End;

Procedure TRecordExtractor.SetOutStr(OutStrP:TStringList);
Begin
OutStr:=OutStrP;
End;


But writes Method "Destroy" hides virtual method of base type "TObject"
This, as I understand it, is normal. We still call it through inherited.

But the question remains. Will they be freed
InStr.free;
OutStr.free;

And another problem
var
RecordExtractor:TRecordExtractor;
...
RecordExtractor:=TRecordExtractor.Create;
freezes (((



Сергей М. ©   (2010-04-05 14:53) [7]


> error understood


Judging by the next dances with a tambourine a niche, you don’t understand it.

override - specifier for overlapping of the same name virtual ancestor methods

The ancestor of your class is the TObject class (it is the progenitor of ALL Delphic classes, without exception).

The TObject.Destroy destructor is just a virtual method, it should be blocked, which the rake hints at you "Method "Destroy" hides virtual method of base type "TObject""

The constructor of TObject.Create is not virtual - it is static class method its must not block what the rake clearly tells youCannot ovveride a static method"



Девушка   (2010-04-05 15:14) [8]


> Sergey M. © (05.04.10 14: 53) [7]

Still used to understand why it hangs ....
Starts to hang if in the designer to declare
 InStr:=TStringList.Create;
 OutStr:=TStringList.Create;


If you do not declare - does not hang ...



Сергей М. ©   (2010-04-05 15:17) [9]


> Girl (05.04.10/15/14 8:XNUMX) [XNUMX]


> Still understand why it freezes

For this, there is just a debugger built into Delphi



icWasya ©   (2010-04-05 15:25) [10]

Girl (05.04.10/15/14 8:XNUMX) [XNUMX]

Still used to understand why it hangs ....

patamushto -
InStr - declared as property
Property InStr: TStringList read GetInStr write SetInStr;
and function
Function TRecordExtractor.GetInStr: TStringList;
Begin
GetInStr: = InStr; // here again we turn to property
End;
and as a result, infinite recursion
InStr-> GetInStr-> InStr-> GetInStr-> InStr-> GetInStr-> InStr-> GetInStr->

Showing

TRecordExtractor=class
 private
   NowString:longint;
// здесть лучше использовать абстрактный класс
   FInStr:TStrings;  
   FOutStr:TStrings;
   Procedure SetInStr(InStrP:TStrings);
   Procedure SetOutStr(OutStrP:TStrings);
 public
   Property InStr:TStrings read FInStr write SetInStr; //входной стринглист
   Property OutStr:Tstrings read FOutStr write SetOutStr;//выходной стринглист (3 строчки или сколько получится)
   Constructor Create;
   Destructor Destroy;
 end;

var
Form1: TForm1;

implementation

{$R *.dfm}

Constructor TRecordExtractor.Create;
Begin
Inherited;
// а вот здесь нужно использовать уже реальный StringList
FInStr:=TStringList.Create;
FOutStr:=TStringList.Create;
NowString:=0;
End;

Destructor TRecordExtractor.Destroy;
Begin
FInStr.free;
FOutStr.free;
Inherited;
End;

// в методах сеттерах лучше использовать assign
Procedure TRecordExtractor.SetInStr(InStrP:TStrings);
Begin
 FInStr.Assign(InStrP);
End;

Procedure TRecordExtractor.SetOutStr(OutStrP:TStringList);
Begin
 FOutStr.Assign(OutStrP);
End;



Find ten differences



cyber-pilot   (2010-04-05 15:31) [11]


> Girl (05.04.10/15/14 8:XNUMX) [XNUMX]
> Still understand why it hangs ....
> It starts to hang, if you declare in the constructor
> InStr: = TStringList.Create;
> OutStr: = TStringList.Create;
>
> If you do not announce-does not hang ...


Recursion is obtained. It would be necessary to write something like this.

TRecordExtractor = class
private
FInStr: TStringList;
FOutStr: TStringList;
NowString: longint;
Function GetInStr: TStringList;
Procedure SetInStr (InStrP: TStringList);
Procedure SetOutStr (OutStrP: TStringList);
Function GetOutStr: TStringList;

public
Property InStr: TStringList read GetInStr write SetInStr; // input stringlist
Property OutStr: TstringList read GetOutStr write SetOutStr; // output stringlist (3 lines or how many will be)
Constructor Create;
Destructor Destroy; override;
end;

was
Form1: TForm1;

implementation

{$ R * .dfm}

Constructor TRecordExtractor.Create;
Begin
FInStr: = TStringList.Create;
FOutStr: = TStringList.Create;
NowString: = 0;
End;

Destructor TRecordExtractor.Destroy;
Begin
FInStr.free;
FOutStr.free;
Inherited;
End;

Function TRecordExtractor.GetInStr: TStringList;
Begin
Result: = FInStr;
End;

Function TRecordExtractor.GetOutStr: TStringList;
Begin
Result: = FOutStr;
End;

Procedure TRecordExtractor.SetInStr (InStrP: TStringList);
Begin
FInStr: = InStrP;
End;

Procedure TRecordExtractor.SetOutStr (OutStrP: TStringList);
Begin
FOutStr: = OutStrP;
End;



Девушка   (2010-04-05 15:37) [12]


Procedure TRecordExtractor.SetInStr(InStrP:TStringList);
Begin
FInStr.Assign(InStrP);
End;

Passes by

Function TRecordExtractor.GetInStr:TStringList;
Begin
Result:=FInStr;
End;

Recurses ((



Девушка   (2010-04-05 15:52) [13]


> icWasya © (05.04.10 15: 25) [10]

Here was the correct answer))
Thank you all.



Pages: 1 whole branch

Forum: "Beginners";
Current archive: 2010.08.27;
Download: [xml.tar.bz2];

Top





Memory: 0.61 MB
Time: 0.125 c
2-1273841049
RGV
2010-05-14 16:44
2010.08.27
WebBrowser


9-1185035010
Evgraf
2007-07-21 20:23
2010.08.27
Prompt


3-1235071724
DmitryG.
2009-02-19 22:28
2010.08.27
Problems with ADO


15-1265188639
12
2010-02-03 12:17
2010.08.27
Those who worked with Oracle and MSSQL. What is more convenient for you?


2-1274516991
ali
2010-05-22 12:29
2010.08.27
Problem with switching to D2010





afrikaans albanian Arabic armenian azerbaijani basque belarusian bulgarian catalan Chinese (Simplified) Chinese (Traditional) croatian Czech danish Dutch English estonian filipino finnish French
galician georgian German greek haitian Creole hebrew Hindi hungarian icelandic Indonesian Irish italian Japanese Korean latvian lithuanian macedonian malay maltese norwegian
persian polish portuguese Romanian russian serbian slovak Slovenian Spanish swahili Swedish ภาษาไทย turkish Ukrainian urdu Tiếng Việt welsh yiddish bengali bosnian
cebuano Esperanto gujarati hausa hmong igbo javanese kannada Khmer lao latin maori marathi mongolian nepali punjabi somali tamil telugu yoruba
zulu
English French German Italian Portuguese Russian Spanish