2

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 :(

copyninja
  • 1,387
  • 2
  • 10
  • 19
  • Are listOfDates and listOfDescription properties of the class? – yehnan Jan 11 '10 at 09:12
  • Yes it is the property of the class with nonatomic and retain – copyninja Jan 11 '10 at 09:54
  • You should not use `self.` at all here. – kennytm Jan 11 '10 at 10:01
  • I think with or without self both should be ok. But I don't know why you get EXEC_BAD_ACCESS with using self. Would you please reveal more code regarding allocation/initialization/deallocation? – yehnan Jan 11 '10 at 11:32
  • I've updated with code for initialization – copyninja Jan 12 '10 at 04:25
  • I assume that you get EXEC_BAD_ACCESS because you over-release something. However in your code I see over-retain(memory leak) not over-release. So I guess the real problem happens in somewhere else. – yehnan Jan 12 '10 at 07:28
  • in initWithDate, the empty array [[NSMutableArray alloc] init] you created does nothing because later you get a mutableCopy of something. Why? – yehnan Jan 12 '10 at 07:30
  • 1
    in initWithDate, self.listOfDates = [dates mutableCopy]; will cause memory leak. You need to release what mutableCopy returns. See http://stackoverflow.com/questions/1087689/who-is-responsible-for-releasing-objects-in-an-array-when-copying/1088943#1088943 for more explanation. I suggest that don't use dot-notation in init and dealloc. – yehnan Jan 12 '10 at 07:32
  • 1
    By the way, you're useing mutable objects as properties. This http://stackoverflow.com/questions/816720/whats-the-best-way-to-use-obj-c-2-0-properties-with-mutable-objects-such-as-nsm/816942#816942 may help. – yehnan Jan 12 '10 at 07:35
  • Yeah i found mutableCopy was causing a leak. I've removed it. before removing mutableCopy as i mentioned in the dealloc if i don't use self. notation no bad access comes – copyninja Jan 12 '10 at 07:52
  • You removed mutableCopy? Don't you need it in initWithDate? – yehnan Jan 12 '10 at 07:59
  • Now i'm just assigning them self.listOfDates = dates; self.listOfDescription = description; – copyninja Jan 12 '10 at 08:14

7 Answers7

2

self is not required for releasing in dealloc function.

Nithin
  • 6,435
  • 5
  • 43
  • 56
2

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
  • 82,288
  • 22
  • 110
  • 138
0

The basic problem you are having is caused by this code:

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

When a property is "retain" and you access it via the setter then the object assigned to it should be in the autorelease pool, like so:

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

But, this is kind of ugly and the NSMutableArray provides a nice constructor that does this for you automatically:

self.listOfDates = [NSMutableArray array];

Your code is not using ARC, so the object should be in the autorelease pool. If you were using ARC then the rules are a little different.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
0

Best Practice(Non ARC). Within your Interface Declare Varibal like below

NSMutableArray *_listOfDates;
NSMutableArray *_listOfDescription;

And your Setters should be below. (as same as your code)

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

In your implementation

@synthesize listOfDates = _listOfDates, listOfDescription = _listOfDescription;

In initialization

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

        NSMutableArray *tempListOfDescription = [[NSMutableArray alloc] init];
        self.listOfDescription = tempListOfDescription;
        [tempListOfDescription release];
    }
}

in dealloac

-(void) dealloc {
    [_listOfDates release];
    [_listOfDescription release];
    [super dealloc];
 }
damithH
  • 5,148
  • 2
  • 27
  • 31
0

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
0

yes,

[object release];

should work fine.

Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
-1

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

futureelite7
  • 11,462
  • 10
  • 53
  • 87
  • Im not sure why his answer was down-voted. You are NOT supposed to use accessor (self.) in dealloc. Straight form Apple. http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html. Perhaps you down voted him because he said "not necessary" instead of "not recommend", but from the looks of it, benzado is incorrect and futureelite is spot on. – Jason Cragun Aug 18 '11 at 16:20