63839

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?

Answer1:

This is a known and solved bug described in QC:

TCombobox gives AV when selecting empty item from dropdown

<hr>

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;

Answer2:

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.

Recommend

  • Telerik Combo Background
  • Google Places API details language not working
  • TimeoutException exception in StackExchange Redis .NET Library
  • Extract Residual Deviance from anova (glm) in R
  • EXCEL VBA - Long Value increasing within subroutines
  • Why won't controls span multiple columns or rows in an Android FireMonkey app?
  • Why does MySql give “Subquery returns more than 1 row” error?
  • Link error when using lex and yacc output in Visual Studio
  • how to get the absolute position of loaded .obj in three.js?
  • Dynamically create temp table based on resultset from SP
  • Notepad++ - delete all lines with certain text
  • iPhone - UIWebView multiple locks
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • Scala split a multi line string by lines that contain all hyphens
  • Sum values in array of hash if they have the same value
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • NSMutableArray Access Issue
  • Typecasting `this` of a base class template to its derived class
  • Titanium doesn't recognize Android SDK on Windows
  • Search function not doing anything
  • Wait for .each() .getJSON request to finish before executing a callback
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • change color of jstree node
  • Counting problem C#
  • Cypher - matching two different possible paths and return both
  • ListItem.Attributes.Add not working
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Inline R code in YAML for rmarkdown doesn't run
  • RectangularRangeIndicator format like triangular using dojo
  • Can Jackson SerializationFeature be overridden per field or class?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • php design question - will a Helper help here?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?