如何在Objective-c中释放传递给协议或委托方法的对象?

时间:2022-08-08 21:32:24

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

如何以及何时发布在UITableViewDelegate中返回的IndexPath对象?

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

何时应该释放在第一个方法中创建的对象NSNumber?如果我使用autorelease或在方法中释放它,我的代理将无法访问它。如果我保持原样,可能会有内存泄漏

2 个解决方案

#1


4  

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

委托调用只是一个普通的方法调用,因此它同步发生。 (如果委托确实想要挂起对象并在以后使用它,那么由委托来保留它.Cocoa中的内存管理的基本规则是每个方法负责保留(以及稍后释放)对象它需要保持。)

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:

假设methodreturningnumber正在返回一个自动释放的对象,则不需要进行任何额外的内存管理;只是:

NSNumber *number=[self methodreturningnumber];
[delegate didSelectItemAtIndex:number];

#2


0  

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:

在我的委托(OBJ1)我有方法:

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

在我的modalWindow(OBJ3)中,我有从它的子视图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:

OBJ4调用上面的方法:

[delegate colorPicker:self didSelectItemAtIndex:atrribute];

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

...........被调用的方法来自对象: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调用OBJ2(将self作为委托传递)OBJ2生成NSNumber X并使用NSNumber X调用OBJ1的方法

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

OBJ1调用presentModalViewController OBJ3并将X设置为OBJ3的属性之一

OBJ3 is displayed properly and can access NSNumberX

OBJ3显示正常,可以访问NSNumberX

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

OBJ3 VievController类子视图OBJ4(视图显示在同一屏幕上)

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

当我点击OBJ4时,它调用OBJ3的方法而不带参数,而OBJ3的地址为NSNumber,但它超出了范围。

..........

..........

#1


4  

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

委托调用只是一个普通的方法调用,因此它同步发生。 (如果委托确实想要挂起对象并在以后使用它,那么由委托来保留它.Cocoa中的内存管理的基本规则是每个方法负责保留(以及稍后释放)对象它需要保持。)

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:

假设methodreturningnumber正在返回一个自动释放的对象,则不需要进行任何额外的内存管理;只是:

NSNumber *number=[self methodreturningnumber];
[delegate didSelectItemAtIndex:number];

#2


0  

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:

在我的委托(OBJ1)我有方法:

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

在我的modalWindow(OBJ3)中,我有从它的子视图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:

OBJ4调用上面的方法:

[delegate colorPicker:self didSelectItemAtIndex:atrribute];

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

...........被调用的方法来自对象: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调用OBJ2(将self作为委托传递)OBJ2生成NSNumber X并使用NSNumber X调用OBJ1的方法

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

OBJ1调用presentModalViewController OBJ3并将X设置为OBJ3的属性之一

OBJ3 is displayed properly and can access NSNumberX

OBJ3显示正常,可以访问NSNumberX

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

OBJ3 VievController类子视图OBJ4(视图显示在同一屏幕上)

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

当我点击OBJ4时,它调用OBJ3的方法而不带参数,而OBJ3的地址为NSNumber,但它超出了范围。

..........

..........