Welcome, Guest. Please login or register.
Did you miss your activation email?
July 30, 2016, 11:48:38 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 17 - Memory Management
| | | |-+ Memory management issues
Pages: [1] Go Down
Author Topic: Memory management issues (Read 6068 times)
Posts: 1

on: December 25, 2010, 09:22:26 PM

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:

Code: (Objective-C)

#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"

[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.  

Code: (Objective-C)

#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;


Code: (Objective-C)
#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];


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?


Last Edit: December 25, 2010, 09:24:30 PM by dkynyc Logged
Hero Member
Posts: 3114

Reply #1 on: December 26, 2010, 01:17:45 PM

When the object gets destroyed (and dealloc gets called), it really means that its memory is released.  That is, it is available for another to object to use.  However, there's no way to know when that memory is recycled.  So valid data can remain there for some time.   So you can get lucky (as you did) or you can get some random data or your program can end up throwing an exception.  There's no real way to know what's going to happen.

Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.