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: Greg Parker <email@hidden>
- Date: Mon, 7 Nov 2011 07:59:43 -0800
On Nov 4, 2011, at 6:40 PM, Don Quixote de la Mancha wrote:
> My code checks that calloc() returns valid pointers for each
> allocation attempted when creating a new grid. If all the allocations
> succeed, I copy the data in the old grid into the center of the new
> one, then free() each array in the old grid.
>
> 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.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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