[DUG] Rare access violations reading listitem.subitems

Peter Ingham ping_delphilist at 3days.co.nz
Sun Aug 19 10:22:31 NZST 2007


EAccessViolation will be raised if the referenced address is invalid.

However it is quite possible that the referenced address is quite valid 
- it just happens to point at some other piece of data that is being 
relied upon.

Code should never rely on an EAccessViolation to trap occasions where it 
is addressing beyond the limits of lists or arrays.

If the access in question is a write, then you an end up with a bug that 
can be excruciatingly difficult to track down (known as a "scribbler").

Cheers


Ross Levis wrote:
> Hmmm.  I thought an EAccessViolation was raised.  In the Delphi help...
> 
> EAccessViolation is the exception class for invalid memory access errors.
> 
> Unit
> SysUtils
> 
> Description
> EAccessViolation is raised when an application
> 
> Dereferences a nil (Delphi) or NULL (C++) pointer.
> Writes to memory reserved for executable code.
> Attempts to access a memory address for which there is no virtual memory
> allocated to the application.
> 
> Ross.
> 
> -----Original Message-----
> From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
> Behalf Of Matthew Comb
> Sent: Sunday, 12 August 2007 20:38
> To: 'NZ Borland Developers Group - Delphi List'
> Subject: RE: [DUG] Rare access violations reading listitem.subitems
> 
> Note that a try except is not the same.
> 
> An access violation is not the same as raising an exception.
> 
> So the approach is definitely different.
> 
> If you want an example try to do this.
> 
> Var
>   Object: Tobject;
> begin
>   Try
>     Showmessage(inttostr(Object.Tag)); //note no create on Object
>   Except
>     Showmessage('There was an exception');
>   End;
> End;
>   
> 
> -----Original Message-----
> From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
> Behalf Of Ross Levis
> Sent: Sunday, 12 August 2007 8:30 p.m.
> To: 'NZ Borland Developers Group - Delphi List'
> Subject: RE: [DUG] Rare access violations reading listitem.subitems
> 
> I have put a try..except block around these now which is as good as your
> code but it doesn't prevent the problem of replacements not occurring which
> is the main problem.
> 
> I also suspected a thread issue initially.  I do have 2 threads that update
> a couple of the SubItems, but I checked these and they are only ever
> accessed using Synchronize, so no problems there.
> 
> It is certainly an odd one.  Cosmin's idea of something running on the users
> computer causing window handles to be recreated could perhaps explain it,
> but it sounds dodgy to me.
> 
> Thanks,
> Ross.
>  
> -----Original Message-----
> From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
> Behalf Of Matthew Comb
> Sent: Sunday, 12 August 2007 20:03
> To: 'NZ Borland Developers Group - Delphi List'
> Subject: RE: [DUG] Rare access violations reading listitem.subitems
> 
> Ross,
> 
> Two things.
> 
> 1. What has access to these items ? Could a thread be altering these items
> out from beneath you  ? (I noticed in a previous email from you in July that
> you are working with threads).
> 2. Why not make the code a little safer
> 
> e.g.
> 
> If (length(Items.SubItems) > 2) then 
> begin
>   Result := StringReplace(Result,'%a',Item.Caption,[rfReplaceAll]);
>   Result := StringReplace(Result,'%t',Item.SubItems[0],[rfReplaceAll]);
>   Result := StringReplace(Result,'%s',Item.SubItems[1],[rfReplaceAll]);
>   Result := StringReplace(Result,'%c',Item.Subitems[3],[rfReplaceAll]);
> End
> Else
> Begin
>   What the hell am I doing trying to access an array element that does not
> exist.
> End;
> 
> 
> 
> I have one user of my software which is experiencing an occasional access
> violation in a routine which is simply reading some data stored in a
> ListView.  The items in the listview are static and not changing at the
> time.  There are always 5 columns of data, the Caption, and 4 SubItems.
> 
> The function simply replaces some parameters in a user formatted string with
> data as follows.
>  
> 
> Not very efficient but nevermind.
> 
> The first line always works.  Item.Caption gets replaced correctly every
> time and never crashes.  If an Access Violation occurs, it's usually on the
> 2nd line, but sometimes this also works, and the crash occurs on the 3rd
> line.  I've not seen a crash on the 4th line.
> 
> The items were loaded at least 20 minutes prior to this function being
> called, so it's not a matter of the ListItem being deleted or created at the
> same time.
> 
> It's completely beyond me.  Are there any known issues with ListViews in
> Delphi 7, or any other ideas?
> 
> Thanks,
> Ross.
> 
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
> 
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
> 
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
> 
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
> 
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject: unsubscribe
> 



More information about the Delphi mailing list