5 ms·
C has no conventions for memory management. You've got malloc, you've got free, and the rest is up to you. Objective-C has an easy to follow pattern, reference
by boucher 17y ago
C has no conventions for memory management. You've got malloc, you've got free, and the rest is up to you.
Objective-C has an easy to follow pattern, reference counting, which is used consistently throughout the language and libraries.
Creating a class is no more (or less) typing than Java or C++. The syntax is different, but harder to remember? Not really convinced of that. There are basically six things to remember:
@interface for declaring a class header
@implementation for declaring a class implementation
@end for ending either
-whatever for an instance method
+whatever for a class method
and [object message] for message passing syntax
- antirez 17y agoThis is exactly the problem. In every reference counting system you need to know if a function will incrRefCount or not the passed argument, if you need to increment it instead before to pass a new object, and things like this. One can try to have conventions but actually it is needed to know the behavior of other code in order to know if objects are reused, if one must pass objects that will be retained, if the object is expected to be already "safe" and will simply used and removed without to retain/release the reference. For instance if you try to write a C extension for Python or Tcl or other systems using reference counting you'll notice that if you don't know every well the internals it can be not obvious when to increment or not the refcount. I use refcount myself in Redis, and again for people not used to Redis internals to implement a new command may not be trivial exactly for this problem of reference counting. Reference counting is not something to expose for a language that aims to be a such an higher level. In short every language with complex OOP system should have some kind of automatic memory management IMHO.
- boucher 17y agoReference counting really isn't that hard to follow. Cocoa's conventions are pretty straightforward. You need to release (or autorelease -- which by the way is great) for every call to alloc, retain, copy (inc. fooCopy), or new (inc. newFoo). Everything else is autoreleased. Exceptions are documented.