<div dir="ltr"><div>Hello,<br><br>In the following program, I get a runtime error I don't understand. This is a stripped down version of the code. This was written in Delphi 1, I'll end up rewriting this in a completely different manner, but for the sake of understanding, could someone explain to me what is wrong here?<br>
<br>The program dynamically creates and frees controls. To make it simple, dynamically created ShapeButtons are inserted as children of a dynamically created Panel, and the code only frees this Panel, hoping that this will trigger freeing the children controls. IIUC, this hasn't changed since Delphi 1 and it is still how it is supposed to work. When I put a breakpoint in line 79, I do see the Panel being freed. Furthermore, if I debug the program in 1.0.14, the program breaks 12 times on wincontrol.inc line 5214, this being triggered by the call to RemoveControl on line 78. I can't make it break on the matching line 5209 in 1.2RC1.<br>
<br>But (and this is my issue) the program triggers a "Duplicate name" exception on line 60. So although the successful v1.0.14 breakpoint in wincontrol.inc seems to mean that the controls were freed, the "Duplicate Name" would mean that this was not true.<br>
<br></div><div></div>Can someone explain what is going on here?<br><br>unit Memoire2;<br><br>{$MODE Delphi}<br><br>interface<br><br> uses<br> Sysutils, Forms, ExtCtrls, Controls, StdCtrls, Classes, Buttons;<br><br> type<br>
<br> TForm1 = class(TForm)<br> btn_New: TBitBtn;<br> procedure DisplayForm ;<br> procedure FormInit (Sender: TObject);<br> procedure btn_NewClick(Sender: TObject);<br> private<br> { Private declarations }<br>
public<br> { Public declarations }<br> end;<br><br> TShapeButton = class(TButton)<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> Col, Lin : integer ;<br>
end;<br><br> var<br> b_Columns : byte ;<br> b_Lines : byte ;<br> Col1, Lin1, Col2, Lin2 : integer ;<br> Form1: TForm1;<br> tp_Panel : TPanel ;<br><br> const<br> FORM_MARGIN = 10 ;<br> MAIN_FRAME_TAG = 999 ;<br>
<br>implementation<br><br> {$R *.lfm}<br><br> procedure TForm1.DisplayForm ;<br> var<br> wh, ww : integer ;<br> procedure SetButton (pc, pl : byte) ;<br> var<br> newbutton : TShapeButton ;<br> wt, wl : integer ;<br>
begin<br> newbutton := TShapeButton.Create (Self) ;<br> wt := (pl-1) * (tp_Panel.Height-1) div b_Lines ;<br> wl := (pc-1) * (tp_Panel.Width-1) div b_Columns ;<br> with newbutton do begin<br> name := 'sb'+inttostr(pc)+'_'+inttostr(pl) ;<br>
caption := '' ;<br> SetBounds (wl, wt, wh, ww) ;<br> end {with} ;<br> newbutton.col := pc ;<br> newbutton.lin := pl ;<br> newbutton.Parent := tp_Panel<br>// tp_Panel.InsertControl (newbutton) { old code, does not work any better }<br>
end ;<br> var<br> c1, l1 : byte ;<br> wc : TControl ;<br> begin<br> c1 := 0 ;<br> while c1 < (ControlCount) do begin<br> if Controls [c1] .Tag = MAIN_FRAME_TAG<br> then begin<br> wc := Controls [c1] ;<br>
RemoveControl (wc) ;<br> wc.Free<br> end {then}<br> else Inc (c1)<br> end {while} ;<br> wh := (Form1.ClientHeight - 2*FORM_MARGIN) div b_Lines ;<br> ww := (btn_New.Left - 2*FORM_MARGIN) div b_Columns ;<br>
if wh < ww { rend les cases carrées }<br> then ww := wh<br> else wh := ww ;<br> tp_Panel := TPanel.Create (Self) ;<br> tp_Panel.ParentColor := TRUE ;<br> tp_Panel.Tag := MAIN_FRAME_TAG ; { tags the control for deletion }<br>
with tp_Panel do begin<br> SetBounds (FORM_MARGIN, FORM_MARGIN, ww*b_Columns, wh*b_Lines) ;<br> BevelOuter := bvNone<br> end {with} ;<br> wh := (tp_Panel.Height-1) div b_Lines - 3 ;<br> ww := (tp_Panel.Width-1) div b_Columns - 3 ;<br>
if wh < ww<br> then ww := wh<br> else wh := ww ;<br> for c1 := 1 to b_Columns do<br> for l1 := 1 to b_Lines do<br> SetButton (c1, l1) ;<br> InsertControl (tp_Panel) ;<br> btn_New.Enabled := TRUE<br>
end;<br><br> procedure TForm1.FormInit (Sender: TObject);<br> begin<br> DisplayForm ;<br> end;<br><br> procedure TForm1.btn_NewClick(Sender: TObject);<br> begin<br> DisplayForm ;<br> end;<br><br>begin<br> b_Lines := 3 ;<br>
b_Columns := 4 ;<br>end.<br><br><div>-- <br>Frederic Da Vitoria<br>(davitof)<br><br>Membre de l'April - « promouvoir et défendre le logiciel libre » - <a href="http://www.april.org" target="_blank">http://www.april.org</a><br>
</div></div>