Memory Release
- (IBAction)goButton:(id)sender { AClass *ourObject; ourObject = [AClass alloc]; // Do stuff with ourObject…. [ourObject release]; ourObject = nil; }
In the second-to-last line, we send ourObject a release message, bringing its reference count down to "0". The last line of code serves two imoprtant purposes. One, it helps us keep track of which variables still point to objects and which don't -- sort of a bookkeeping convenience. Its second purpose is more practical in that it helps us avoid problems associated with inadvertently sending messages to objects that no longer exist.Sending a message to a nonexistent object will crash your application with a signal 10 (SIGBUS) or signal 11 (SIGSEGV) while your application won't even flinch at messages sent to nil. If you find yourself getting these kinds of crashes, check to make sure you're not trying to use an object that doesn't exist. (I've found in my experimentation that attempting to write to a nonexistent object will produce a Signal 11 segmentation violation, while attempting to retreive data from a nonexistent object will result in a Signal 10 bus error).