• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory leak if alloc succeeds but init fails?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory leak if alloc succeeds but init fails?


  • Subject: Re: Memory leak if alloc succeeds but init fails?
  • From: Wincent Colaiuta <email@hidden>
  • Date: Mon, 1 Oct 2007 17:33:27 +0200

El 1/10/2007, a las 16:45, Jeremy Hughes escribió:

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.

I guess my comments were made in the context of writing init methods, and the decision of how to indicate failure (by throwing or by returning nil). My general practice in that context is to return nil if something outside of my control went wrong (for example, super returned nil), throw an exception if there's a programmer error, and in most cases allow exceptions thrown by super or other classes that I interact with to fall through (this really falls under the "exceptional or unexpected behaviour" you talk about). But obviously this is just a general pattern and it can vary in specific cases; the most important thing is to uphold your end of your published API contract (as documented).


For example, C++ memory allocation automatically throws an exception if
the memory could not be allocated. There is no result that you can check.

Obviously; you need to code to the API and the language you're working with. That means fulfilling required preconditions, passing the expected arguments, checking for documented return values, and being prepared to catch documented exceptions.


Cheers,
Wincent

_______________________________________________

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


References: 
 >Re: Memory leak if alloc succeeds but init fails? (From: Wincent Colaiuta <email@hidden>)
 >Re: Memory leak if alloc succeeds but init fails? (From: "Jeremy Hughes" <email@hidden>)

  • Prev by Date: Design question: NSFIleHandle and NSFileHandleDataAvailableNotification
  • Next by Date: Where are the boundaries of Cocoa and how are the boundaries perceived ?
  • Previous by thread: Re: Memory leak if alloc succeeds but init fails?
  • Next by thread: mail-in db/app
  • Index(es):
    • Date
    • Thread