Try/Catch a segmentation fault on Linux
Solution 1
If you have a scenario where many pointers across your app reference the same limited-lifetime objects, a popular solution is to use boost smart pointers. Edit: in C++11, both of these types are available in the standard library
You would want to use shared_ptr
for pointer(s) that are responsible for the lifetime of your object and weak_ptr
for the other pointers, which may become invalid. You'll see that weak_ptr
has the validity check you're asking for built in.
Solution 2
A segmentation fault is not an Exception (like Java's NullPointerException); it is a signal sent from the OS to the process. Have a look at the manpage for sigaction for pointers on how to install a handler for the segmentation fault (SIGSEGV).
Solution 3
You could enable a signal handler for SIGSEGV for this one occurrence. See the man page "signal" for details. The other alternative is to use references which are guaranteed to be valid. It depends on your application of course.
Solution 4
Initialize your pointer to NULL. If after some processing it is still NULL it’s invalid, otherwise it’s valid.
Solution 5
There is no natural, universal way with raw C++ pointers. C++ assumes that you will track that information.
In most situations, you can handle this by remembering to set pointers to NULL when they are invalid. New pointers that initially don't point should be set to NULL and newly deleted objects should have their pointers set to NULL.
Related videos on Youtube
jackhab
Updated on September 16, 2020Comments
-
jackhab over 3 years
I have a Linux C++ application and I'd like to test an object pointer for validity before dereferencing it. However try/catch doesn't work for this on Linux because of the segmentation fault. How can this be done?
-
jackhab over 15 yearsOn Windows you can catch an exception when accessing a pointer to deallocated object and you can deal with the situation. On Linux you get SIGSEGV. My question is there any way to validate a pointer that once pointed to a an object and hence is not NULL but still invalidated by delete operator.
-
jackhab over 15 yearsIf I delete an object it's pointer is not NULL but points to invalid memory. How can I validate such pointer?
-
Bombe over 15 yearsWhy the hell would you even want to? You deleted it. If you are using pointers anywhere and you’re not sure whether they’re deleted or not you are doing something VERY wrong.
-
jackhab over 15 yearsThe man page says a the process state is undefined if it ignores the SIGSEGV. Can I resume C++ application after catching SIGSEGV?
-
Greg Dan over 15 yearsI am not Win32 expert but once I used __try __except for such things.
-
gimpf over 15 yearsI second that. Invalid pointers are invalid pointers. Either afterwards the pointer-variable is not accessible anymore, or you set it to 0. Or, as this is C++, learn about RAII.
-
gimpf over 15 yearsReferences are also not guaranteed to be valid. If the lifetime of the object referenced ends before the reference is used, the results are also undefined (in practice identical to an invalid pointer). References cannot be initialized to null, and not reset. That's it.
-
Greg Reynolds over 15 yearsTrue - but if you are using them "normally" then they are less likely to give you a problem. I should have been a bit clearer in that you can set a pointer to any old rubbish, but a reference (at least at first) is a lot more difficult to get wrong. I blame my nasty cold for slow wits :-(.
-
jackhab over 15 yearsI haven't used shared_prt but as far as I understand it is a pointer which controls object's life time. I have opposite situation. I have an object which can delete itself upon receiving a message. I need those pointing to it to have a way to know they possess invalid pointer.
-
timday over 15 yearsThat's exactly the sort of case weak_ptr is made for. The weak_ptr will let the object die, and report invalidity if you later try and access the object through the weak_ptr. A much better solution than catching SIGSEGV!
-
timday over 15 yearsThose are windows-only things, exposing the platform's "Structured exception handling" (SEH) in C++. The linux equivalent is signals... but I think Shmoopty's suggestion of shared_ptr/weak_ptr is more along the right lines.
-
Drew Dormann over 15 yearsThat's what I thought your situation was. The "delete me" message should reset the one-and-only shared_ptr. After that, all weak_ptr's made from that shared_ptr will know they are invalid. Take a look at the weak_ptr link for examples.
-
Andrew over 3 years