Below are three fragments of code, the first two are interfaces for two objects along with a couple of @property commands开发者_JAVA技巧. 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
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.
you must call release by hands for each time you've called retain or alloc method.
Yes, by setting it to nil or calling release depending on OS.
精彩评论