How does D handle null pointers?

Adam Ruppe destructionator at gmail.com
Fri Aug 20 15:25:15 PDT 2010


This is brought up somewhat often on this group. Problem with throwing
an exception in a segfault is that it is undefined behavior in POSIX.

However, I encountered exactly your problem: a null pointer in a big web app.

The way I solved it was to sprinkle

assert( var !is null );

before I start using or storing the thing, and in the contracts to the
functions. Those assertations throw exceptions with a stack trace, but
if you compile in release mode, they disappear. I now know if it is
null well before the actual deference - back when the null was
assigned. This makes finding the root cause of the problem easy, much
easier than even tracking down a segfault in the debugger.


More information about the Digitalmars-d mailing list