Re: Allocating too much memory kills my App rather than returning NULL
Re: Allocating too much memory kills my App rather than returning NULL
- Subject: Re: Allocating too much memory kills my App rather than returning NULL
- From: Wade Tregaskis <email@hidden>
- Date: Mon, 07 Nov 2011 12:38:41 -0800
>> If calloc() ever returns NULL while attempting to allocate the new
>> grid, I back out all of the successful allocations by passing each
>> array pointer to free(). Then I display an alert to the user to
>> advise them that there is not enough memory for their desired grid
>> size, and suggest trying a smaller one.
>
> This is not a safe pattern in a multi-threaded application. You may be checking for failed allocations and responding correctly, but to a close approximation no code anywhere else does so. For example, objc_msgSend sometimes needs to allocate memory, and has no way to recover if it cannot. If some other thread were working at the same time you caused allocation failures, then that thread is likely to crash.
'course, that's just one defeatist's opinion. As a counter-opinion, I think that handling runtime errors is wise, and I've seen many crashes averted in practice (in multi-threaded code) in cases just like this.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden