tags:

views:

152

answers:

5

Hi all, I've some doubts regarding dealloc function in iPhone program. Is it required to give [self.object_name release] or [object_name release] is ok?.. I've this strange problem in my dealloc function is like this.

-(void) dealloc {
   [self.listOfDates release];
   [self.listOfDescriptions release];
   [super dealloc];
 }

But program crashes giving EXEC_BAD_ACCESS. Here both objects are NSMutableArray instances allocated with alloc in init function for the class. The same function without self works fine i.e

-(void) dealloc {
    [listOfDates release];
    [listOfDescription release];
    [super dealloc];
 }

Here is how I declared the property

@property (nonatomic,retain) NSMutableArray *listOfDates;
@property (nonatomic,retain) NSMutableArray *listOfDescription;

In the implementation file I sysnthesized this and inside the init function I've allocated these variables like this

self.listOfDates = [[NSMutableArray alloc] init];
self.listOfDescription = [[NSMutableArray alloc] init];

So is it required to give self ? What am I missing here?

Issue resolved when I removed mutableCopy function which I had used to copy instance of NSMutableArrays which were passed as argument to the init function as shown below

-(id)initWithDate:(NSMutableArray *)dates andDescription:(NSMutableArray*)descriptions
{
    if(self = [super initWithNibName:@"DateDescriptionControl" bundle:nil])
    {
        self.listOfDates = [[NSMutableArray alloc] init];
        self.listOfDescription = [[NSMutableArray alloc] init];

        self.listOfDates = [dates mutableCopy];
        self.listOfDescription = [description mutableCopy]; 

    }

   return self;
}

After removing the mutableCopy the dealloc is now not throwing EXEC_BAD_ACCESS. So where have I made the mistake I still can't figure out :(

A: 

self.something means [self something] since the dot means property syntax. What you want is self->something.

KennyTM
`self->something` is valid but unnecessary and strange.
benzado
It's the minimal change if the asker want to keep the `self`. Of course the 2nd code in the question is preferred.
KennyTM
+2  A: 

self is not required for releasing in dealloc function.

Nithin
A: 

yes,

[object release];

should work fine.

Sam Jarman
A: 

Just use [object release] to release the instance variable. Using "self" is not necessary.

futureelite7
Incorrect. In dealloc, `self` isn't invalid yet.
benzado
+2  A: 

In dealloc you have two choices:

[foobar release];

or

self.foobar = nil;

The second one is equivalent to writing [self setFoobar:nil] and it is inside the setFoobar: method is where the previous value is being released (assuming the property was defined as using retain or copy). I tend to prefer the first form, where you just send release directly to the object, but either will do.

Writing [self.foobar release] should technically be OK, although if you later call self.foobar = nil the object will be released a second time (and cause EXC_BAD_ACCESS).

benzado