[fpc-pascal] with no if assigned() there are bugs

Michael Van Canneyt michael at freepascal.org
Wed Sep 21 08:06:26 CEST 2016



On Tue, 20 Sep 2016, Lars wrote:

> Hi all, I am wondering what your thoughts are on this idea: add the
> ability to do if assigned() on all objects to prevent bugs, maybe as a
> compiler feature. Before you write me off as just another person
> requesting yet another silly feature (it may in fact be that), here is the
> problem:
>
> A lot of "other people's code" I deal with, has access violations. When I
> try to debug the application code, I waste hours of my time, only to find
> out that it was that common bug: a person forgot to check
>
>  if assigned(SomeObject)
>
> before accessing the object properties or fields.  i.e. the access
> violation was because of referencing an invalid (non existing) object.
>
> So what if the compiler had an option to check if assigned, before
> accessing any object. This would seem to be like a performance hit however
> I'm not sure that an If check is that much of a waste of CPU power. It
> would be similar to range checking: yes, may be a performance hit, but
> increases safety of application.

This would in general be a very bad idea.

If there is no 'If Assigned()', it means the person who programmed it,
assumes the object is always valid at that point. 
If it is not, you should ask yourself why it is not valid, 
and not hide the problem by adding "if Assigned()" automatically.

The -CR command-line option more or less does this, but I am not sure if it
also checks field or property access.

Michael.



More information about the fpc-pascal mailing list