Ankit Sachan
Ankit Sachan

Reputation: 7840

iphone how to release memory in this case

I have a method like this

- (NSDictionary *)getCellValuesForRow:(int)row {
NSMutableDictionary *dictValues= [[NSMutableDictionary alloc] init];

Outage *outage = [listOutage objectAtIndex:row];

[dictValues setObject:outage.duration forKey:@"OutageDuration"];
   return dictValues;

}

and this value is stored in this way

NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[self getCellValuesForRow:(indexPath.row-1)]];

how to release memory in this scenario

Upvotes: 2

Views: 77

Answers (3)

Joseph Lin
Joseph Lin

Reputation: 3334

An alternative is to simply use

NSMutableDictionary *dictValues= [NSMutableDictionary dictionary];

That's effectively the same thing as what Dan suggested. Just less typing.

It applies to your next line, too:

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[self getCellValuesForRow:(indexPath.row-1)];

Upvotes: 0

DenverCoder9
DenverCoder9

Reputation: 3705

You should autorelease dictValues in getCellValuesForRow, or just don't alloc it. This will keep it autoreleased:

NSMutableDictionary *dictValues= [NSMutableDictionary dictionary];

In most cases it should be the responsibility of whatever calls it to alloc it (if it needs to be kept around after the autorelease pool is cleared), then dealloc it later.

If whatever calls it doesn't need it kept around, it can just leave it autoreleased.

Upvotes: 1

Dan Ray
Dan Ray

Reputation: 21893

This is what autorelease is for.

NSMutableDictionary *dictValues= [[[NSMutableDictionary alloc] init] autorelease];

Upvotes: 2

Related Questions