Delphi / Windows组合框中的空字符串导致访问异常 [英] Empty string in Delphi / Windows combo box causes access exception

查看:68
本文介绍了Delphi / Windows组合框中的空字符串导致访问异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi 7.0应用程序,它每次在与组合框关联的字符串列表中写入一个空字符串时,都会引发一个内存访问异常/消息框:

I've got a Delphi 7.0 application that throws a memory access exception / message box every time it writeln's an empty string from the string list associated with a combo box:

csvstrlst := combobox1.Items;

csvstrlst.clear;
csvstrlst.add('');    //problem
csvstrlst.add('a');   //no problem
csvstrlst.add('');    //problem
csvstrlst.add('b');   //no problem

//throws memory access messages (I think the writeln writes a line though)
for n := 1 to csvstrlst.Count do begin
    writeln(out_file,csvstrlst.strings[n-1])
end;

//throws memory access messages (writeln does write a comma text string though)
writeln(out_file,csvstrlst.commatext);

在Windows 7或XP上运行。作为应用程序或在D7 IDE中。如果更改了包含其字符串的表单的父级,则带有空字符串项的组合框也会导致相同的错误。

Running on Windows 7 or XP. As application or in D7 IDE. Combobox with empty string items also causes the same error if the parent of the form it is on is changed.

有没有其他人见过或听说过此问题?

Has anyone else ever seen or heard of this problem? Any other information available at all?

推荐答案

这是QC中描述的已知和已解决的错误:

This is a known and solved bug described in QC:

从下拉列表中选择空项目时,TCombobox会显示AV a>

尽管这是一个错误,但您不应重用控件中的部件来执行某些数据任务,如您所描述的问题。

Although this is a bug, you should not reuse parts from controls to perform some data tasks as described in your question.

这样做不会节省任何时间,而是会在大多数时间获得不必要的副作用(控件会重新绘制和/或发生火灾)

You will not save anything doing so, but getting most the time unwanted sideeffects (controls get repainted and/or fire events)

如果要拥有 TStringList ,则创建一个实例。

If you want to have a TStringList then create an instance.

csvstrlst := TStringList.Create;
try

  // csvstrlst.Clear;
  csvstrlst.Add( '' );
  csvstrlst.Add( 'a' );
  csvstrlst.Add( '' );
  csvstrlst.Add( 'b' );

  for n := 0 to csvstrlst.Count - 1 do 
  begin
    WriteLn( out_file, csvstrlst[n] )
  end;

  WriteLn( out_file, csvstrlst.CommaText );

finally
  csvstrlst.Free;
end;

这篇关于Delphi / Windows组合框中的空字符串导致访问异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆