1. release with the specified owner:
// Uses VCL. stdctrls, VCL. extctrls; var panel: tpanel; Procedure tform1.button1click (Sender: tobject); begin panel: = tpanel. create (Self); panel. parent: = self; with tbutton. create (panel) Do // aowner = panel begin parent: = Panel; caption: = 'new button'; end; Procedure tform1.button2click (Sender: tobject ); begin if assigned (panel) Then freeandnil (panel); // release the end object whose name is the owner;
Ii. interfaces used:
Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs, VCL. stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; // ibass = interface function getname: string; Procedure setname (const aname: string); property name: String read getname write setname; end; // tbass = Class (tinterfacedobject, ibass) Private fname: string; protected function getname: string; procedure setname (const aname: string); Public constructor create (const aname: string); end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); var X: ibass; begin X: = tbass. create ('wanyi'); showmessage (X. name); X. name: = 'case'; showmessage (X. name); {x automatically released here} end; {tbass} constructor tbass. create (const aname: string); begin fname: = aname; end; function tbass. getname: string; begin result: = fname; end; Procedure tbass. setname (const aname: string); begin fname: = aname; end.
Iii. Usage structure:
Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs, VCL. stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; // tbass = record private fname: string; Procedure setname (const aname: string); Public constructor create (const aname: string); property name: String read fname write setname; end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); var X: tbass; begin X: = tbass. create ('wanyi'); showmessage (X. name); X. name: = 'case'; showmessage (X. name); {x automatically released here} end; {tbass} constructor tbass. create (const aname: string); begin fname: = aname; end; Procedure tbass. setname (const aname: string); begin fname: = aname; end.
4. Create in initialization and release in finalization:
Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs, VCL. stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var list: tstringlist; Procedure tform1.button1click (Sender: tobject); begin list. clear; List. add ('wanyi'); showmessage (list. text); end; Initialization list: = tstringlist. create; finalization list. free; end.
5. Use Dynamic Arrays instead of tlist and tstringlist:
Procedure tform1.button1click (Sender: tobject); var arr: array of string; I: integer; s: string; begin for I: = 0 to 6 do begin setlength (ARR, length (ARR) + 1); arr [high (ARR)]: = stringofchar (char (I + 65), 3); end; for s in arr do showmessage (s); end; Procedure tform1.button2click (Sender: tobject); var arr: tarray <string>; I: integer; s: string; begin for I: = 0 to 6 do begin setlength (ARR, length (ARR) + 1); arr [high (ARR)]: = stringofchar (char (I + 65), 3 ); end; for S in arr do showmessage (s); end;
6. Use tobjectlist instead of tlist:
Uses system. contnrs; Procedure tform1.button1click (Sender: tobject); var list: tobjectlist; I: integer; BTN: tbutton; begin list: = tobjectlist. create; for I: = 0 to 6 do begin BTN: = tbutton. create (Self); with BTN do begin caption: = format ('btn % d', [I + 1]); parent: = self; top: = height * I; left: = width * I Div 2; end; List. add (BTN); end; showmessage ('objects will be released when tobjectlist is released '); list. free; end; Procedure tform1.button2click (Sender: tobject); var list: tlist; I: integer; BTN: tbutton; begin list: = tlist. create; for I: = 0 to 6 do begin BTN: = tbutton. create (Self); with BTN do begin caption: = format ('btn % d', [I + 1]); parent: = self; top: = height * I; left: = width * I Div 2; end; List. add (BTN); end; showmessage ('objects are not released' after tlist is released); list. free; end;
7. Use tobjectlist instead of tlist:
Uses system. generics. collections; Procedure tform1.button1click (Sender: tobject); var list: tobjectlist <tbutton>; I: integer; BTN: tbutton; begin list: = tobjectlist <tbutton>. create; for I: = 0 to 6 do begin BTN: = tbutton. create (Self); with BTN do begin caption: = format ('btn % d', [I + 1]); parent: = self; top: = height * I; left: = width * I Div 2; end; List. add (BTN); end; showmessage ('objects will be released when tobjectlist is released '); list. free; end; Procedure tform1.button2click (Sender: tobject); var list: tlist <tbutton>; I: integer; BTN: tbutton; begin list: = tlist <tbutton>. create; for I: = 0 to 6 do begin BTN: = tbutton. create (Self); with BTN do begin caption: = format ('btn % d', [I + 1]); parent: = self; top: = height * I; left: = width * I Div 2; end; List. add (BTN); end; showmessage ('objects are not released' after tlist is released); list. free; end;
8. Use struct instead of struct pointer:
{Assume that a function parameter requires a structure pointer} function area (rect: prect): integer; begin result: = rect. width * rect. height; // result: = rect ^. width * rect ^. height; end; {manually release the pointer and allocate space} procedure tform1.button1click (Sender: tobject); var P: prect; begin new (p); P ^: = rect (10, 10, 60, 50); showmessage (inttostr (area (p); // 2000 dispose (p); End ;{} procedure tform1.button2click (Sender: tobject); var R: trect; Begin R: = rect (10, 10, 60, 50); showmessage (inttostr (area (@ r); // 2000end;