Why does this crash ?

A long while back I wrote a quick post about “if you need to crash your app” and how to

Perhaps you need to test some code that reports crashes back to you

Exceptions are easier to catch. crashes a bit harder but they happen

So I had posted a couple lines I got from Joe a zillion years ago

Dunno if anyone ever used them or needed them until recently. Of course it was asked on Xojo’s forums so I cant answer there but I did get the answer to the asker.

And now there is a follow up about WHY does this cause a crash ?

Well lets look at a crash log on macOS from it. on my machine an app with nothing but those two lines in the Window1.open event gives this crash log (I havent included the entire thing just the relevant portions)

Process:               My Application.debug [49041]
Path:                  /private/var/folders/*/My Application.debug.app/Contents/MacOS/My Application.debug
Identifier:            com.mycompany.myapp
Version:               ??? (1.0.0.0.0)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           My Application.debug [49041]
User ID:               501

Date/Time:             2021-04-02 12:13:31.871 -0600
OS Version:            Mac OS X 10.15.7 (19H1026)
Report Version:        12
Bridge OS Version:     5.3 (18P54555a)
Anonymous UUID:        0F0B83EB-BB66-3337-7E31-C659E43657EB


Time Awake Since Boot: 440000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [49041]

VM Regions Near 0:
--> 
    __TEXT                 0000000102f15000-0000000103064000 [ 1340K] r-x/rwx SM=COW  /private/var/folders/*/My Application.debug.app/Contents/MacOS/My Application.debug

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   My Application.debug          	0x00000001030483aa Window1.Window1.Event_Open%%o<Window1.Window1> + 122 (/Window1:49)
1   XojoFramework                 	0x00000001033b3d69 FireWindowOpenEvents + 231
2   My Application.debug          	0x0000000102f65f0c Window.Constructor%%o<Window> + 60
3   My Application.debug          	0x00000001030487cd Window1.Window1%o<Window1.Window1>% + 589 (/Window1:61)
4   My Application.debug          	0x000000010304e02a _MakeDefaultView + 106 (/#main:57)
5   My Application.debug          	0x000000010304e28f _LateStartup + 79 (/#main:90)
6   XojoFramework                 	0x000000010321ed1d CocoaFinishApplicationStartup() + 185
7   XojoFramework                 	0x000000010321d97f 0x103134000 + 956799

That this causes a segmentation fault isnt entirely surprising
But why does it do that ?

Well when we start the Ptr is uninitialized. So its DEFAULT value is 0.And then we try to write to offset 0 what this tries to do is WRITE to offset 0 from address 0 of the application and this is NOT permitted.
And so we get

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)

which in THIS case is an attempt to either READ or WRITE to a READ ONLY portion of the application. Most modern processors support this kind of low level protection to protect against malicious software that would modify itself in memory to do harmful things AND avoid detection since what is on disk isnt what being executed.

In this case the ptr is nil (or 0) and trying to write to a ptr that points at 0, or NIL, fails spectacularly.

In fact the relevant code could also be

dim p as ptr
dim i as integer = p.Byte(0)

and it will crash – again because trying to read or write to address 0 fails spectacularly

One Reply to “Why does this crash ?”

  1. I caught mention of your you post

    “If you ever need to crash” – June 26, 2019

    on a Xojo forum thread and was curious about what was going on behind the scenes to cause the crash. That’s an interesting way to get your application terminated by the OS. Thanks for the explanation.

Comments are closed.