Re: NSMutableData capacity
Re: NSMutableData capacity
- Subject: Re: NSMutableData capacity
- From: Jens Alfke <email@hidden>
- Date: Wed, 23 May 2012 12:49:46 -0700
On May 23, 2012, at 12:15 PM, Jason Teagle wrote:
> Fair point, and well noted. I think it's a shame that objects would be so cavalier about their existing state (isn't that just sloppy? Makes it far too easy to leak memory)
No, it's part of the contract of object initialization. Initializer methods may only be called on 'blank' objects returned from +alloc. They're like constructors in C++/Java, just not built into the language*.
If you want a method to _reset_ an existing object, that's something different. Some classes have this, like -[NSMutableArray removeAllObjects]. It fundamentally doesn't make sense for immutable objects like NSString, however, since it could change their contents.
I guess you could say it's 'easy' to leak memory by calling -init more than once, but only in the sense that it's 'easy' to leak by calling -retain too many times (without ARC), or to crash by writing past the end of a data's -mutableBytes. Objective-C isn't a totally safe language like Java or Ruby. :)
—Jens
* Keep in mind that Objective-C has been since the early '80s and was primarily influenced by Smalltalk-80; the alloc/init dichotomy comes directly from Smalltalk. C++ existed at the time but hadn't become mainstream yet.
_______________________________________________
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