Re: Memory leak if alloc succeeds but init fails?
Re: Memory leak if alloc succeeds but init fails?
- Subject: Re: Memory leak if alloc succeeds but init fails?
- From: "Jeremy Hughes" <email@hidden>
- Date: Mon, 1 Oct 2007 15:45:59 +0100
- Organization: Softpress
Wincent Colaiuta (1/10/07, 15:23) said:
>>> Exceptions should really only be used to signal programmer errors;
>>> that is failed assumptions (things you assume to be true).
>>>
>>> So I would say, return nil if the failure to initialize is caused by
>>> an external factor outside of your control, and raise an
>>> exception if your assumptions about what state should be true have
>>> not been met.
>>
>> This is a subjective assessment, easily contradicted. I think the
>> failure
>> of alloc to return me any memory would be 'an external factor
>> outside of my
>> control', but would not have expected it to happen. Its definitely
>> something that I cannot 'pre-check'.
>
>I totally agree with you. The success or otherwise of a memory
>allocation is something outside of your control, something whose
>success you should not "assume" and you should therefore check the
>result. I never said otherwise.
The original comment (exceptions should only be used to signal
programmer errors) is pretty questionable. Exceptions are designed to be
used for any exceptional or unexpected behaviour, whether caused by a
programmer error or by a system error.
For example, C++ memory allocation automatically throws an exception if
the memory could not be allocated. There is no result that you can check.
Jeremy
_______________________________________________
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