Skip to content

Greetings: House Cleaning

Text in subscriptObject-C is like C++ a language that does not provide an automatic memory management like Java’s Garbage Collection and so we need to take care of that ourselves. This might sound bad especially when you are a C/C++ developer but it is not that bad in our case but it is important to get into the habit and so we start right here.

Rules

The memory management rules in Object-C are fairly simple:

  • If you own an object you have to release it
  • If you don’t own it then you ignore it
  • You own it by either:

    • Alloc
    • New
    • Copy
    • Retain (meaning you make an object your own that wasn’t so far)

Now that leaves us with one problem on how to manage objects we cannot control anymore because we return it back to a caller. Well, in that case we can mark the object to be autoreleased which means it is going to be destroyed later. The receiver of the object can decide if it wants to retain the object and in that case he becomes the owner.

In general we want to deallocate any object (if not already done) in the dealloc method. In our application we have created two Outlets which we need to destroy when necessary:

1
2
3
4
5
- (void)dealloc {
    [greetLabel release];
    [greetInput release];
    [super dealloc];
}