急等:动态数组释放问题所有源码贴出,哪位帮看一下 ( 积分: 100 )

  • 主题发起人 主题发起人 czly
  • 开始时间 开始时间
C

czly

Unregistered / Unconfirmed
GUEST, unregistred user!
rdgOption:为Tradiogroup动态数组

var
i, j, k: Integer;
begin
inherited;
for k := low(rdgOption) to high(rdgOption) do
begin
rdgOption[k].Free;
rdgOption[k] := nil;
end;
itemSysID := rgItemSys.ItemIndex + 1;
openDataSetEx(DM_Frm.AdoConn, adsEval,'SELECT COUNT(*)'
+' FROM EVALUATE_SYSTEM_DETAIL WHERE EVAL_SYS_ID ='+quotedstr(intToStr(ItemSysID)));
{ TODO : 得到题数 }
evaluate_basic_Count := adsEval.Fields[0].AsInteger;
setLength(rdgOption, evaluate_basic_Count);
openDataSetEx(DM_Frm.AdoConn, adsEval,'select k.id,k.name as lkey,k.eval_id,'
+ 'k.percentage,b.name,s.Percentage as mark from evaluate_key as k,'
+ 'evaluate_basic as b, EVALUATE_SYSTEM_DETAIL as s where b.id ='
+ ' k.Eval_id and b.id =S.Eval_Id and Eval_Sys_id ='+ quotedstr(intToStr(ItemSysID)));
{ TODO : 创建题目和答案 }
for j := 1 to evaluate_basic_Count do
begin
dataSetLocate(adsEval,'eval_id',j);
rdgOption[j] := TRadioGroup.Create(self);
rdgOption[j].Width := 420;
rdgOption[j].Height := 50;
rdgOption[j].Columns := 4;
rdgOption[j].Left := (sbcontainer.Width - rdgOption[j].Width) div 2;
rdgOption[j].Top := RdgOption[j].Height * j;
rdgOption[j].Caption := intToStr(j)+'. ' + adsEval['name'];
rdgOption[j].Tag := adsEval['eval_id'];
rdgOption[j].ItemIndex := -1;
rdgOption[j].Parent := sbcontainer;
for i := 1 to 4 do
begin
rdgOption[j].Items.Add(adsEval['lkey']);
adsEval.Next;
end;
end;
end;
 
rdgOption:为Tradiogroup动态数组

var
i, j, k: Integer;
begin
inherited;
for k := low(rdgOption) to high(rdgOption) do
begin
rdgOption[k].Free;
rdgOption[k] := nil;
end;
itemSysID := rgItemSys.ItemIndex + 1;
openDataSetEx(DM_Frm.AdoConn, adsEval,'SELECT COUNT(*)'
+' FROM EVALUATE_SYSTEM_DETAIL WHERE EVAL_SYS_ID ='+quotedstr(intToStr(ItemSysID)));
{ TODO : 得到题数 }
evaluate_basic_Count := adsEval.Fields[0].AsInteger;
setLength(rdgOption, evaluate_basic_Count);
openDataSetEx(DM_Frm.AdoConn, adsEval,'select k.id,k.name as lkey,k.eval_id,'
+ 'k.percentage,b.name,s.Percentage as mark from evaluate_key as k,'
+ 'evaluate_basic as b, EVALUATE_SYSTEM_DETAIL as s where b.id ='
+ ' k.Eval_id and b.id =S.Eval_Id and Eval_Sys_id ='+ quotedstr(intToStr(ItemSysID)));
{ TODO : 创建题目和答案 }
for j := 1 to evaluate_basic_Count do
begin
dataSetLocate(adsEval,'eval_id',j);
rdgOption[j] := TRadioGroup.Create(self);
rdgOption[j].Width := 420;
rdgOption[j].Height := 50;
rdgOption[j].Columns := 4;
rdgOption[j].Left := (sbcontainer.Width - rdgOption[j].Width) div 2;
rdgOption[j].Top := RdgOption[j].Height * j;
rdgOption[j].Caption := intToStr(j)+'. ' + adsEval['name'];
rdgOption[j].Tag := adsEval['eval_id'];
rdgOption[j].ItemIndex := -1;
rdgOption[j].Parent := sbcontainer;
for i := 1 to 4 do
begin
rdgOption[j].Items.Add(adsEval['lkey']);
adsEval.Next;
end;
end;
end;
 
如果这样释放数组rdgOption := nil;再次创建时又出现
Invalid pointer operation
 
出现了什么问题?
 
释放再次创建出错误
当选了另一个radiogroup组件的item时
单击创建,执行这个过程
第一次不出错,选radiogroup组件的另一item时 ,再点创建 出现错误
因为几次的evaluate_basic_Count 值不同,数组创建和释放出了问题
 
为什么不用list?
 
楼主在使用动态数组前没使用setlength设定长度~~
 
1.在用动态数组前,用 SetLength 过程为数组分配内存。
2.如果每次用动态数组的大小都不同,只要用 SetLength 过程为数组重新分配内存即可。
3.动态数组是生存期自管理的,所以在用完它们以后没有必要释放,因为在离开作用域时它们会被释放。然而,可能在离开作用域前,就需要删除动态数组(例如它用了很多内存)。要这么做,仅需要把 nil 赋值给动态数组:
rdgOption:=nil; //释放 rdgOption
4.动态数组通常是以0为基准的。
 
可那并不能解决
 
var
i, j, k: Integer;
begin
inherited;
for k := high(rdgOption) downto low(rdgOption) do //这样写应该就可以解决问题
begin
rdgOption[k].Free;
rdgOption[k] := nil;
end
 
for k:=0 to num do
begin
New(rdgOption[k]);
rdgOption[k]^:= TRadioGroup.Create(self);
with rdgOption[k]^ do
begin
....
.... //初始化
end;
end;
for k:=0 to num do
begin
rdgOption[k]^.Free;
dispose(rdgOption[k]^);
end;
 
//我把你的数据库部分去掉了,因为我没有数据库
//调试正确,你可以做相应修改
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
RadioGroup1: TRadioGroup;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
var
rdgOption : array of Tradiogroup;

procedure TForm1.Button1Click(Sender: TObject);
var
i, j, k: Integer;
begin
setLength(rdgOption, 5);//setlength 要放到数组操作的最前面,
for k := low(rdgOption) to high(rdgOption) do
begin
rdgOption[k].Free;
rdgOption[k] := nil;
end;
{itemSysID := rgItemSys.ItemIndex + 1;
openDataSetEx(DM_Frm.AdoConn, adsEval,'SELECT COUNT(*)'
+' FROM EVALUATE_SYSTEM_DETAIL WHERE EVAL_SYS_ID ='+quotedstr(intToStr(ItemSysID)));
{ TODO : 得到题数 }
//evaluate_basic_Count := adsEval.Fields[0].AsInteger;
//setLength(rdgOption, evaluate_basic_Count);//这个须要放到前面
//openDataSetEx(DM_Frm.AdoConn, adsEval,'select k.id,k.name as lkey,k.eval_id,'
// + 'k.percentage,b.name,s.Percentage as mark from evaluate_key as k,'
// + 'evaluate_basic as b, EVALUATE_SYSTEM_DETAIL as s where b.id ='
// + ' k.Eval_id and b.id =S.Eval_Id and Eval_Sys_id ='+ quotedstr(intToStr(ItemSysID)));
{ TODO : 创建题目和答案 }
for j := 0 to 4 do
begin
//dataSetLocate(adsEval,'eval_id',j);
rdgOption[j] := TRadioGroup.Create(self);
rdgOption[j].Width := 420;
rdgOption[j].Height := 50;
rdgOption[j].Columns := 4;
rdgOption[j].Left := (self.Width - rdgOption[j].Width) div 2;
rdgOption[j].Top := RdgOption[j].Height * j;
rdgOption[j].Caption := intToStr(j);//+'. ' + adsEval['name'];
// rdgOption[j].Tag := adsEval['eval_id'];
rdgOption[j].ItemIndex := -1;
rdgOption[j].Parent := self;
for i := 1 to 4 do
begin
rdgOption[j].Items.Add('test');
// adsEval.Next;
end;
end;
end;

end.
 
for k:=0 to num do
begin
New(rdgOption[k]);
rdgOption[k]^:= TRadioGroup.Create(self);
with rdgOption[k]^ do
begin
....
.... //初始化
end;
end;
for k:=0 to num do
begin
rdgOption[k]^.Free;
dispose(rdgOption[k]); //上次该处写错了
end;
 
看看jj
应该是j+1
 
ak2002,nc2t的方法最简单,用Tlist也可以,分数不多,只乘几十分了,等当专家后再给大家吧!
数组的low为0

for k := Low(rdgOption) to High(rdgOption) do
rdgOption[k].Free;
for j := 0 to evaluate_basic_Count - 1 do :)原来写了1
~~~~
begin
dataSetLocate(adsEval,'eval_id',j + 1);
rdgOption[j] := TRadioGroup.Create(self);
~~~~~
rdgOption[j].Width := 420;
rdgOption[j].Height := 50;
rdgOption[j].Columns := 4;
rdgOption[j].Left := (sbcontainer.Width - rdgOption[j].Width) div 2;
rdgOption[j].Top := RdgOption[j].Height * (j + 1);
rdgOption[j].Caption := intToStr(j + 1)+'. ' + adsEval['name'];
rdgOption[j].Tag := adsEval['eval_id'];
rdgOption[j].ItemIndex := -1;
rdgOption[j].Parent := sbcontainer;
for i := 1 to 4 do
begin
rdgOption[j].Items.Add(adsEval['lkey']);
adsEval.Next;
end;
end;
 
后退
顶部