Hello all, I have been taking a wack at practicing some objective c code. I ran into the follow issue where an object that had its memory released still returns values assigned to the object. Here is the code:
#import <Foundation/Foundation.h>
#import "Movie.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Movie *movie = [[Movie alloc] initWithTitle:@"Iron Man"
andRating:5
andYear:2009];
[movie play];
//NSLog(@"Our movie is %@", movie);
NSLog(@"The retain count is: %d", [movie retainCount]);
[movie release];
NSLog(@"The retain count is: %d", [movie retainCount]);
[movie play];
[pool drain];
return 0;
}
Here are the header and implementation files for the movie class.
#import <Cocoa/Cocoa.h>
@interface Movie : NSObject {
NSString *title;
int rating;
int year;
}
@property(assign) NSString *title;
@property(assign) int rating;
@property(assign) int year;
-(id)initWithTitle: (NSString *)newTitle
andRating: (int)newRating
andYear: (int)newYear;
-(void)setTitle:(NSString *)title;
-(void)play;
@end
#import "Movie.h"
@implementation Movie
@synthesize title;
@synthesize rating;
@synthesize year;
-(id)initWithTitle: (NSString *)newTitle
andRating: (int)newRating
andYear: (int)newYear {
self = [super init]; // Call NSObject init
if (nil != self) { // Check if NSObject init was nil
self.title = newTitle;
self.rating = newRating;
self.year = newYear;
}
return self;
//title = newTitle;
//rating = newRating;
}
-(NSString *)description {
NSString *oldDescription = [super description];
return [NSString stringWithFormat:@"%@ title = %@, rating = %d, year = %d",
oldDescription, self.title,self.rating,self.year];
}
-(void)setTitle:(NSString *)newTitle {
title = [newTitle capitalizedString];
}
-(void)play {
NSLog(@"Playing %@ - Stars = %d - Year = (%d)", self.title, self.rating, self.year);
}
-(void)dealloc {
NSLog(@"dealloc called");
[super dealloc];
}
@end
Unsure what the issue is but I can't seem to get the movie object to release.
After the [movie release] method is called, I call the [movie play] method and it still returns the movie title, rating, and year?
Can anyone chime in on what is wrong?
Thanks