Empty string in Delphi / Windows combo box causes access exception

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);

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?


This is a known and solved bug described in QC:

TCombobox gives AV when selecting empty item from dropdown


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)

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;


As Sir Rufo has discovered the issue is a VCL bug introduced in Delphi 7 as described in QC#2246. According to that report the bug is resolved in a build with major version number 7 so you may be able to fix the problem by applying the latest Delphi 7 updates.

If not then you can fix the problem from the outside. I don't actually have a Delphi 7 installation to test this on, but I believe that this interposer class will work.

type TFixedComboBoxStrings = class(TComboBoxStrings) protected function Get(Index: Integer): string; override; end; TComboBox = class(StdCtrls.TComboBox) protected function GetItemsClass: TCustomComboBoxStringsClass; override; end; function TFixedComboBoxStrings.Get(Index: Integer): string; var Len: Integer; begin Len := SendMessage(ComboBox.Handle, CB_GETLBTEXTLEN, Index, 0); if (Len <> CB_ERR) and (Len > 0) then begin SetLength(Result, Len); SendMessage(ComboBox.Handle, CB_GETLBTEXT, Index, Longint(PChar(Result))); end else SetLength(Result, 0); end; function TComboBox.GetItemsClass: TCustomComboBoxStringsClass; begin Result := TFixedComboBoxStrings; end; <hr>

The bug that was introduced in Delphi 7 is simply that the if statement reads:

if Len <> CB_ERR then

So, when Len is zero, that is when the item is the empty string, the True branch of the if is chosen. Then, the SendMessage becomes:

SendMessage(ComboBox.Handle, CB_GETLBTEXT, Index, Longint(PChar('')));

Now, PChar('') has special treatment and evaluates to a pointer to read only memory containing a zero character. And so when the combo box window procedure attempts to write to that memory, an access violation occurs because the memory is read only.

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Empty string in Delphi / Windows combo box causes access exception