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

Lars noreply at z505.com
Wed Sep 21 07:29:41 CEST 2016


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.

If you downloaded some code and had an access violation, the first thing
you would do is enable the compiler option to always if assigned() check
automatically on all objects, recompile the app, and if the bug goes away
you know it was because !someone! forgot to check "if assigned()" on an
object before accessing it.

Would this be a good safety feature, in the spirit of range checks, and in
the spirit of avoiding C like code (buffer over runs) because essentially
this is unsafe C like programming to access a dead object, IMO.

With garbage collection you don't have this problem (maybe it creates
other issues such as reusing objects you didn't really want to use). 
However as FPC and Delphi are likely not going to be garbage collected for
some time, if ever, wouldn't this If Assigned check add one more reason to
use FPC without the nasty access violations caused by non garbage
collected programming?

I am open to being debated on this: I'm not 100 percent certain this
feature would be as good as I make it out to be. Would it be hard to
implement? I don't know.. an if check, automatically, before accessing an
object....   Then also would you do performance optimizations to only call
if assigned() once if a bunch of object fields are being accessed in a row
such as:

stringlist.add('one')
stringlist.add('two')

Only one if assigned() check is needed before the above two calls, not a
check each time stringlist.add is called. This performance optimization
could be a worry later, and the first time if assigned() is implemented
automatically by the compiler, performance is ignored.  Plus this feature
might just be a debug facility rather than something you enable when you
ship the application.... similar to range checking (although some would
argue range checking should ALWAYS be on and turning it off is silly).

What are your thoughts? Thing is, I've come across more than my fingers
can count, of times that this bug has bit me, especially when I am
downloading someone elses' code and haven't gone through thousands of
source files looking to see that if assigned() was called.



More information about the fpc-pascal mailing list