3 ms·
Why do apps get killed for requesting memory? Can't malloc return NULL and the app act accordingly ("your picture is too big, so i am not going to edit it").
by jrockway 16y ago
Why do apps get killed for requesting memory? Can't malloc return NULL and the app act accordingly ("your picture is too big, so i am not going to edit it").
Now, it seems most people never handle malloc returning NULL, and "you can't do this today" is a bad user experience. But crashing is what happens in the first case anyway, and being auto-killed is a bad experience too.
- rbritton 16y agoI don't know. But it'll usually give you the memory, send the level 2 memory warning (level 3 is kill), and then kill you shortly after. The memory warning is supposed to be a warning to release cached data, so it's sort of hard to release the data you're working on right at that point in time gracefully.
- Zev 16y agoApps don't get killed for requesting memory. They get killed when they're running in the background and other apps request memory. Can't malloc return NULL and the app act accordingly ("your picture is too big, so i am not going to edit it"). Thats exactly what happens. You're free to run: void *foo = malloc(INT_MAX); foo will be NULL. The rest of the app will continue to run without any problems. // edit: Here's what happens when you do run that (the last line is a NSLog() to show the address of foo): Malloc(4972,0x3e088868) malloc: *** mmap(size=2147483648) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug 2010-09-12 19:44:59.154 Malloc[4972:307] malloc: 0x0 // edit 2: Here's the code: http://thisismyinter.net/misc/hn/Malloc.zip http://thisismyinter.net/misc/hn/Malloc.zip