<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Win API returns -1 to indicate “an error”. <b><u>Any</u></b>
error. Not a specific error. Certainly not specifically a list index error.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Unfortunately, the Win API return value is ALSO the “Item data”
value. So if you store -1 and get -1 back it <i>could</i> be an error or it <i>could</i>
be the valid result.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>The VCL <b><u>assumes</u></b> that if it gets -1 back then it
must be an error. It then makes matters worse by further <b><u>assuming</u></b>
that it must be a list index error.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>What it <b><i><u>should</u></i></b> do is check the list index
provided *<b>itself</b>* BEFORE sending that index to the WinAPI and then simply
give the caller back whatever value the WinAPI yields (or at least raise
LastOSError, not some assumed exception class). This is a case of the VCL
trying to be too helpful and making a botched job of it in the process.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>And as I pointed out, it is now (as of Delphi 2006 at least)
fixed (poorly, imho, but still effectively fixed) in comboboxstrings, just not
listboxstrings.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>It is similar to the error handling in <b>TWinControl.GetDeviceContext()</b>
that naively assumes that a <b>GetDCEx()</b> result of 0 (zero) must indicate
an <b>EOutOfResources</b> condition. That bit of “intern code” cost me many
lost hours trying to track down a resource leak when the true problem was
actually an invalid window handle !!!<b><o:p></o:p></b></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'>
<div>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>
delphi-bounces@delphi.org.nz [mailto:delphi-bounces@delphi.org.nz] <b>On Behalf
Of </b>Kyley Harris<br>
<b>Sent:</b> Tuesday, 16 February 2010 2:51 p.m.<br>
<b>To:</b> NZ Borland Developers Group - Delphi List<br>
<b>Subject:</b> Re: [DUG] Is it a bug in latest version of Delphi?<o:p></o:p></span></p>
</div>
</div>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>interesting<o:p></o:p></p>
<div>
<p class=MsoNormal><o:p> </o:p></p>
</div>
<div>
<p class=MsoNormal>> if Longint(Result) = LB_ERR then
Error(SListIndexError, Index);<o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><o:p> </o:p></p>
</div>
<div>
<p class=MsoNormal>it would seem that the error is not in delphi at all. Well..
they are simply raising the Windows error which say that -1 is the error result
for a bad index access from windows. if delphi were to ignore this error and
you asked for the value at index[99] which was out of bounds and you got -1
instead of an error I think there would be the same number of complaints from
the other side. <o:p></o:p></p>
</div>
<div>
<p class=MsoNormal><o:p> </o:p></p>
</div>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'>Clearly its the windows API
that considers -1 invalid.. not delphi.<o:p></o:p></p>
<div>
<p class=MsoNormal>On Tue, Feb 16, 2010 at 2:24 PM, Karl Reynolds <<a
href="mailto:kreynolds@chocfishsoft.co.nz">kreynolds@chocfishsoft.co.nz</a>>
wrote:<o:p></o:p></p>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'>On Tue, Feb 16, 2010 at 1:55
PM, Alister Christie<br>
<<a href="mailto:alister@salespartner.co.nz">alister@salespartner.co.nz</a>>
wrote:<br>
> I think the point I was trying to make is that it is that the method<br>
> (/array indexer or whatever) was expecting a TObject - passing anything<br>
> else could result in unexpected results.<o:p></o:p></p>
</div>
<p class=MsoNormal>Only if you stipulate that you have to remain ignorant of
the function<br>
internals. The TObject is converted back and forth from a LongInt in<br>
order to store it anyway (see GetObject and PutObject).<o:p></o:p></p>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'><br>
> The fact that -1 doesn't work<br>
> is therefor not a bug (unless you can have 1 byte sized objects).<o:p></o:p></p>
</div>
<p class=MsoNormal>var x: longint;<br>
<br>
x := -1; // Four bytes<br>
<br>
(May the source be with you).<br>
<br>
Cheer,<br>
<span style='color:#888888'>Karl</span><o:p></o:p></p>
<div>
<div>
<p class=MsoNormal>_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@delphi.org.nz">delphi-request@delphi.org.nz</a>
with Subject: unsubscribe<o:p></o:p></p>
</div>
</div>
</div>
<p class=MsoNormal><br>
<br clear=all>
<br>
-- <br>
Kyley Harris<br>
Harris Software<br>
+64-21-671-821<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>