为什么出现“List index out of bounds (-1)”错误?(50分)

Z

zouming

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么出现“List index out of bounds (-1)”错误,共3次,
最后出现“Invalid pointer operation”错误。
delphi3下编程。
 
1: 在访问列表时索引溢出
2:非法的指针操作
看看你的代码
 
比如ListBox.items[-1],这时候就超出范围了。
 
但只是偶尔出现,大概连续编译运行几十次后就会出现,why?
 
出现这个错误的时候是超出范围了,但偶尔出现的话,就看你具体的算法了。
 
是否是delphi控件的bug?
 
看看你的代码
 
同意blue_morning,的看法
 
如果是偶尔出现且两个错误信息同时出现:说明你的某个列表对象已经被释放了,而你仍引用这个对象
 
但然没有-1这列!
 
DataModuleForm.Table_CostCount.actived:= false情况下,是否如下代码有问题?
procedure TDataModuleForm.Table_CostCountCalcFields(DataSet: TDataSet);
begin
Table_CostCount.FieldByName('RowNo').AsInteger:=DataModuleForm.Table_CostCount.RecNo;
end;
 
Table都已经Close了,如何存取字段值?这样做不出错才怪呢
 
if Table_CostCount.Active then
begin
Table_CostCount.FieldByName('RowNo').AsInteger:=DataModuleForm.Table_CostCount.RecNo;

end;
 
已删除了如下代码还是解决不了问题。
如果出现问题会出现死机现象,只能关闭delphi3,才能退出,why?
procedure TDataModuleForm.Table_CostCountCalcFields(DataSet: TDataSet);
begin
Table_CostCount.FieldByName('RowNo').AsInteger:=DataModuleForm.Table_CostCount.RecNo;
end;
 
我大多数时候是出现在用循环遍历操作时候
比如,统计出来的EDIT有22个,但是循环却写成
FOR I:=0 TO 22 成了23个
应该是I:=0 TO 22-1
 
DataModuleForm.Table_CostCount和Table_CostCount是同一张表吗? 为什么要加不一样
的前缀呢?
我在D5中测试了一下,我是这样写的:
procedure TForm1.Table_CostCountCalcFields(DataSet: TDataSet);
begin
Table_CostCount.FieldByName('RecNo').AsInteger := Table_CostCount.RecNo;
end;
另外应当不存在Active为False时会异常的可能,因为事件是写在计算字段计算值时发生,
Active一定是为True的。
  如果你是多次操作后才发生索引溢出的问题你是不是将表中的记录删除了?你在删除操作后
用Close, Open再试试。
如果你不确定发生错误的代码在这里可以用
try
Table_CostCount.FieldByName('RecNo').AsInteger := Table_CostCount.RecNo;
except
showmessage('在显示行号时出错')
end;
来试试, 进而找到出错的地方。
 
1、OnCalcFields事件当你Active=False时是不会触发的,所以不是这段代码的问题,
2、你确认Table_CostCount.FieldByName('RecNo')是计算字段吗?
3、List index out of bounds (-1)应该跟这个无关,你应该跟踪一下确定出错位置。
 
用了如下代码,忘了释放,是不是此原因?
procedure TProjectTemplateSelectForm.FormCreate(Sender: TObject);
begin
File_New_Open_Off;
// in MenuAndSpeedButton_OnOff.pas
{1、“常用”页设置)
{把图标加入Large和Small ImageList 中}
imgListLarge:=TImageList.CreateSize(32,32);
imgListSmall:=TImageList.Create(Self);
largeIcon:=TIcon.Create;
largeIcon.LoadFromFile(MainDir+'/SpaceLarge.ico');
imgListLarge.AddIcon(largeIcon);
smallIcon:=TIcon.Create;
smallIcon.LoadFromFile(MainDir+'/SpaceSmall.ico');
imgListSmall.AddIcon(smallIcon);
ListView_SpaceProject.LargeImages:= imgListLarge;
ListView_SpaceProject.SmallImages:= imgListSmall;
{加入图标}
with ListView_SpaceProject.Items.Adddo
begin
Caption:='空工程项目';
ImageIndex:=0;
end;

{2、“模板”页设置}
TemplateList(ListView_TemplateProject, MainDir + '/Template/');
//in Templet.pas
end;
 
应该是内存释放问题,请及时使用free。
 
listbox.items[x] x超出范围  !
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
916
DelphiTeacher的专栏
D
顶部