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: Jeff Laing <email@hidden>
- Date: Mon, 1 Oct 2007 09:36:34 +1000
> 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'.
> throws an NSInternalInconsistencyException). But if there is
> something outside of your control (eg. you need to read a file but
> you find that you don't have access permission) you release yourself
> and return nil.
I can see where the same code path might fail for expected and unexpected
reasons which you cannot discern from within the method.
a) create a new object, initialising from a file that the user just selected
b) create a new object, initialising from a file whose name I recorded
earlier and stored in my 'uber-document'
The object initialiser cannot know that in the first case, the file not
existing would be evidence of a bug whereas in the second, its the result of
the user having rearranged his file system.
_______________________________________________
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