Search code examples
delphidelphi-7

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?


Solution

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