views:

25

answers:

2

How and when is released IndexPath object which is returned in UITableViewDelegate?

let's imagine code:

//my method in my object
{
    NSNumber *number=[[NSNumber alloc] init];
    number=[[self methodreturningnumber] retain];

    [delegate didSelectItemAtIndex:number];
}

in my delegate I have method:

-(void)didSelectItemAtIndex:(NSNumber *)number
{
}

when object NSNumber created in first method should be released? If I use autorelease or release it in method it will not be accessible in my delegate. If I leave it as it is, there may be memory leak

+1  A: 

Just release it after you call the delegate method (or use autorelease).

The delegate call is just a normal method call, so it happens synchronously. (And if the delegate does want to hang on to the object and use it later, it's up to the delegate to retain it. The cardinal rule of memory management in Cocoa is that each method is responsible for retaining (and later releasing) the objects it needs to keep around.)

Also, note that you have another memory leak in your example: you are allocing number, and then assigning another value to it (leaking the initial value).

Assuming methodreturningnumber is returning an autoreleased object, you don't need to do any additional memory management; just:

NSNumber *number=[self methodreturningnumber];
[delegate didSelectItemAtIndex:number];
David Gelhar
I changed this, but how to retain this object later? I described issue below.
plusz
A: 

I just tested it after change, it works, but isse appears on the next step if I try to use this variable later, it is lost.

I changed objects names to make analysis easier:

//my method in my object  OBJ2
{
[delegate didSelectItemAtIndex:[self methodreturningnumber]];
}

in my delegate (OBJ1) I have method:

-(void)didSelectItemAtIndex:(NSNumber *) NSNUMBER
{


SliceDetailsViewController * OBJ2 = [[SliceDetailsViewController alloc] initWithNibName:nil bundle:nil];
OBJ2. NSNUMBER = NSNUMBER;    //          <----------  THIS IS LOST IF I DON'T ADD RETAIN   [NSNUMBER retain]
OBJ2.pieChart = graphView;
OBJ2.myColors=myColors;

[self presentModalViewController:sliceDetailsController animated:YES];

[sliceDetailsController release]; 
}

in my modalWindow (OBJ3) I have method which is called from it's subview OBJ4

- (void)colorPicker:(ColorPicker *)myColorPicker didSelectItemAtIndex:(NSNumber *)indexPath
{
[OBJ2 setColor:NSNUMBER value:indexPath];     //      <------ SLICEID IS NOT AVAILABLE HERE IF NOT RETAINED IN PREVIOUS OBJECT

}

OBJ4 calls this method above:

[delegate colorPicker:self didSelectItemAtIndex:atrribute];

........... called methods are from objects: OBJ1-->OBJ2-->OBJ1-->OBJ3-->OBJ4-->OBJ3

OBJ1 call OBJ2 (passing self as delegate) OBJ2 generates NSNumber X and call method of OBJ1 with NSNumber X

OBJ1 call as presentModalViewController OBJ3 and sets X as one of attributes of OBJ3

OBJ3 is displayed properly and can access NSNumberX

OBJ3 VievController class subview OBJ4 (view displayed on the same screen)

when I click on OBJ4 it calls method of OBJ3 without arguments and OBJ3 has address of NSNumber, but it is out of scope.

..........

plusz
I wonder if initial method where I create object may have any impact on this:NSNumber *numSliceId=[[NSNumber alloc] init];numSliceId=[NSNumber numberWithFloat:12345];return [numSliceId autorelease];
plusz