Wednesday, November 3, 2010

Hello again!In this follow-up I will explain the allocators and destructors available on Objective-C.

alloc

alloc allocates memory for an object of a given class, but DOES NOT initialize this object. Usually, alloc comes together with some initializer like initWithString or simply init. For example:

NSString*foo=[[NSStringalloc]initWithString:@"foo"];

new

new is a convenience method equivalent to [[Class alloc] init]. This is not documented though, and was subject of a thread on cocoa-dev mailing list back in 2008. Bill Bumgarner, who is an Apple Software Engineer, explained it on the list:

Bill Bumgarner:I just checked all the way back to 10.1. The implementation was +allocWithZone: NULL followed by -init until Leopard, when it moved to +alloc followed by -init.

copy

This is the keyword for the copy constructor. It allocates memory and initializes an object with the copy of the sender. Example:

A class must implement the NSCopying Protocol in order to use the copy constructor. This is accomplished by completing 2 steps:1. Extend NSObject <NSCopying> instead of NSObject, for example.2. Implement the copyWithZone method.

Note: The implementation above uses @property and @synthesize just to handle the setters and getters. I am going to write a tutorial about properties later on, but for now just imagine that " copy.attribute_string = " is replaced by copy->setAttributeString(...), and setAttributeString is a public method that deals with the string copying and releasing.

retain

retain is used to get ownership over a reference you didn't allocate yourself. A common usage of retain is to hold references of objects inside collections.

Usually, core collections, like NSArray, NSDictionary and their mutable counterparts, release all objects once the container is released. This can create invalid references, if one needs to retain a reference to an object of the container for longer than the container itself. For example:

autorelease

autorelease is a kind of lazy-release. When an object receives the autorelease message, the object ownership is transferred to the last created autorelease pool, thus extending the lifetime of the object and simplifying memory management.

An autorelease pool is a space where all references to objects that received an autorelease message are stored. When the pool is drained (same as deallocated), these references receive a release message and then gets deallocated.

Therefore, there are 2 basic implications of using autorelease: Either use release or autorelease, and never both. Always make sure you have an autorelease pool created before you send an autorelease message, or Cocoa will log an error.

Autorelease pools can be nested and are stored in a stack. This means that one can't extend the lifetime of an object, using autorelease, more than the last created pool's lifetime. Also, this mean that if you are not sure an autorelease pool was created, you should always create an autorelease pool before using autorelease.

And finally, one should use autorelease when he needs to create an object inside a scope, that will be used outside this scope.

Saturday, October 30, 2010

Hello everyone! This is my first post on this blog and I hope you enjoy it.

On this post I will introduce the concepts behind memory management in Objective-C. This will be the first of a serie of posts about memory management, where I intend to cover everything you need to know to be confident with pointers in Objective-C. :)

Ownership

To understand memory allocation in Objective-C, we must first understand Ownership. Ownership principle states that for each object one creates, one is the only responsible for destroying it. If one doesn't destroy it, then there will be a memory leak. And if someone else destroys it, then there will be bugs, crashes and all sorts of unexpected behaviors due to invalid references being used.

In practice, the Ownership principle states that for each new there should be a delete on a C++ code, for example.

This principle is implemented on compilers via reference counting. For each allocator call, the reference count of an object is incremented and for each destructor call this reference count is decremented. When the reference count reaches 0, the memory space is cleared and set as available for other allocations.

In Objective-C, there are 4 keywords for allocators: alloc, new, copy, retain and 2 for destructors: release and autorelease.

Thursday, October 28, 2010

This is the first Lab about ObjectARX for Mac programming. In this Lab you will learn how to create a new project inside XCode, how to compile and build it. At the end, you will learn how to debug the application using XCode debug tools.

If you have any problem watching the embedded video try the following link: