Crash Protection for Xojo methods

Sometimes you may have a situation, where you want to try something, that may crash. This may be a problem outside your control like a system or library function. Or some pointer reading may cause a page fault and you like to try it. Or some code produces crash reports for your users and you like to catch it and show a bug reporter dialog to get more details on why it crashes in a method.

We got recently a CallDelegateCrashSafeMBS module with a CallDelegateCrashSafeMBS method, that calls a method on your behalf. If it crashes, we raise a DelegateCrashExceptionMBS to inform you about it. The error message may tell the error like: “Delegate crashed with invalid pointer access.”

When the crash happens on macOS and Linux, we print a stack trace on the console. That may help if you run the application from Terminal window. Then you can see the stack trace printed. The stack trace within the exception is from the point where it is raised, but that is not the point where the crash happened.

Here is some example code, where we crash within a test method with a nil pointer:

Sub Run()
		System.DebugLog "Try the call..."
		CallDelegateCrashSafeMBS AddressOf Test
		System.DebugLog "Call succeeded!"
	Catch c As DelegateCrashExceptionMBS
		System.DebugLog "Caught exception!"
		System.DebugLog c.Message
	End Try
End Sub
Sub Test()
	System.DebugLog CurrentMethodName
	Dim p As ptr = Nil
	p.Int32(0) = 0 // <-- crashes
	System.DebugLog CurrentMethodName+" done"
End Sub

Please try. Maybe you have a need for some crash protection?

New class included in current MBS Plugin 22.6pr1.


There is an improvement coming for next version:

  • Detect some stack corruption
  • Catch C++ exceptions
  • Catch signal trace trap
    So we may provide a bit more security when calling code you can’t control.