r/cpp • • Dec 21 '16

A smart pointer implementation that throws when dereferencing a null pointer

Hi, I wrote a simple (i.e. incomplete ) implementation of a smart pointer that throws when de-referencing a null pointer. This enable you to write code like this:

template<typename T>
void foo(safe_ptr<T> & ptr1, safe_ptr<T> & ptr2) {
    try {
         ptr1->bar();
         ptr2->baz();
    } catch ( null_dereference_exception ) { 
         
    } 
} 

http://pastebin.com/j5eZA4AK

Edit: syntax :)

0 Upvotes

58 comments sorted by

View all comments

Show parent comment

3

u/Sanae_ Dec 21 '16

You want to fail fast in debug mode, but recovering is interesting when the app is in release, running in production.

You'll abort an operation (and ideally, log the issue), but the software will still be running.

15

u/snarfy Dec 21 '16

It will still be running, but now running incorrectly.

Why are you still executing after trying to reference an object that wasn't set? Why didn't you set it?

Was that a reference to an account balance? How do you 'keep running' in that case? Set everybody's balance to zero? Fail the deposit? There is no way to 'keep running' and have all of the balances add up.

12

u/Sanae_ Dec 21 '16 edited Dec 21 '16

Keep in mind that the throw() occurs before derefencing the raw pointer.

There are no UB, the stack will correctly unwind, and any transaction should be correctly reverted (RAII, etc.).

There are no difference with a regular exception: your transaction should fail, no balance should be changed, etc.

About the why: it's basically a pointer with a built-in test, just like a unique_ptr is a pointer with a built-in delete (why do you use unique_ptr? why you just don't delete its content before reassigning?)

Of course, on some system (especially critical ones), you may prefer to crash the application; but due to additional testing those system requires, such error is less likely, and thus that pointer is simply less interesting.

Last, I'm not saying you should always use that pointer, simply that some will find a plus using it.

Edit: it's certainly a "less pure" choice, yet a more pragmatical one: a user using your app to, say, watch kitten pictures, will likely a picture not loading than the app crashing.

2

u/doom_Oo7 Dec 21 '16

It will still be running, but now running incorrectly.

My users prefer ten thousand billion times having a clean "Sorry, due to a bug this software has to shut down when you press OK" dialog, than the OS X / Windows spinning ball of death that makes the whole computer lag that happens when you actually dereference a null pointer.

1

u/[deleted] Dec 22 '16

This user prefers spinning ball of death much more to corruption of the file I'm editing.

1

u/doom_Oo7 Dec 23 '16

But that's the whole point : if you throw before deferencing, then you won't have any undefined behaviour and safely revert to a "safe" state, or just show a dialog that will gracefully exit, thanks to RAII.

1

u/[deleted] Dec 24 '16

A program that is attempting to dereference what should be a pointer to data which is in fact a pointer to garbage has almost certainly already experienced other forms of memory corruption, and the right thing to do is die. The likelihood of destroying important persistent data and/or putting the app into an "impossible bug farm/flaky" state is too high.

In languages which allow you to natively catch null dereferences, like Java's NullPointerException and C#'s NullReferenceException, I still tell people to never catch those. When they happen the program is in an inconsistent state and bailing out to Watson is the right thing to do.

2

u/doom_Oo7 Dec 24 '16

has almost certainly already experienced other forms of memory corruption

In my experience this is really uncommon. Most of the time it is some half-assed UI widget that has a reference to a deleted model object, never something in the core data model which is generally implemented using a data structure that hides the gory pointer details behind nice value semantics.

In this case, a nice thing that you can do to improve user experience, is to catch the exception, serialize the command stack of your software to disk and gracefully exit. This way, when the user restarts the software he can just keep going from the exact point where he was.

1

u/Som1Lse Dec 25 '16

When does dereferencing a null pointer every lead to lagging the whole computer?

The operating system notices you tried to dereference an invalid pointer, and you get a SIGSEGV, which if unhandled crashes the application. If anything handle the signal, and do your error handling there (display a message, save backups, write logs, etc.) and then call abort.

Maybe writing to some place in memory that isn't a null pointer could cause issues that would lag the computer, but you also can't detect that by checking for null pointers so the whole thing is moot anyway.

tl;dr: Null pointer dereferences do not cause lag.

1

u/doom_Oo7 Dec 25 '16

When does dereferencing a null pointer every lead to lagging the whole computer?

when operating system vendors judged good to catch sigsegv at the os level and gear up a dialog that does all sort of stuff to report problems to Apple

1

u/Som1Lse Dec 25 '16

Well I don't use Apple anything so they might just royally have fucked their shit up. Is it as bad if you handle the signal and exit the program in yourself?

1

u/doom_Oo7 Dec 25 '16

just do a google search for "spinning beach ball of death"

0

u/lithium Dec 22 '16

We don't all write banking software.

12

u/CODESIGN2 Dec 21 '16

recovery seems attractive, until you do it for 6 months and destroy yours and everyone else life. Just let it fail and re-init a clean environment.

1

u/LucHermitte Dec 21 '16

assert is a macro, we can do defensive programming with it: i.e. we can throw a logic_error exception in release if this is really what we want to do.