views:

171

answers:

3

Below are three fragments of code, the first two are interfaces for two objects along with a couple of @property commands. Please note that this is just me learning, its not part of an application just a small test program.

After adding (retain) to the 2nd @property I noticed that my machine object was leaking when my program exited, what I did to fix this was add a release in the Artist objects dealloc, does this sound right?

@interface Machine : NSObject
{
    NSString *name;
    NSString *type;
    NSString *os;
}

@interface Artist : NSObject
{
    NSString *name;
    Machine *system;
}
@property(copy) NSString *name;
@property(retain) Machine *system;
@end

// DEALLOC IN @implementation Artist
-(void)dealloc {
    NSLog(@"_deal: %@", self);
    [system release];
    [super dealloc];
}

gary

+2  A: 

you must call release by hands for each time you've called retain or alloc method.

Morion
Thank you, I know that "alloc" "new" "copy" and retain that you are responsible for relinquishing ownership of object. I was just not sure is retain in this situation followed the same rule, obviously it does.
fuzzygoat
A: 

Yes, by setting it to nil or calling release depending on OS.

Mobs
You mean “depending on whether GC is turned on”. You can have GC turned off (and therefore need to release it) on either OS.
Peter Hosey
+3  A: 

If you are targeting Mac OS X, the best answer would be to turn on garbage collection and delete the -dealloc method.

To answer the specific question, you need to release both name and system in your -dealloc method.

bbum
So as far as name goes the "release" in dealloc is to release the copy made in @property(copy).
fuzzygoat
Exactly; `(copy)` and `(retain)` properties imply a `-retain` on set which needs to be balanced by a `-release` in `-dealloc`.
bbum